// スマホの判定
const isMobile = (() => {
// 省略(適当に作る)
})();
/**
* @param {Object} [opts]
* @param {string} [opts.root]
* @param {string} [opts.spDir]
*/
const deviceRedirect = (opts = {}) => {
const { root, spDir } = Object.assign({ root: '/', spDir: 'sp/' }, opts);
const { pathname } = location;
if(pathname.match(new RegExp(`^${ root }${ spDir }`))) {
if(isMobile) return;
} else {
if(!isMobile) return;
}
const _before = isMobile ? `^${ root }` : `^${ root }${ spDir }`;
const _after = isMobile ? (root + spDir) : root;
const _pathname = pathname.replace(new RegExp(_before), _after);
const { protocol, host, search, hash } = location;
location.href = `${ protocol }//${ host }${ _pathname }${ search }${ hash }`;
};