Created
January 4, 2017 05:10
-
-
Save oikwsat/43e34aba9f792eded17010a76ec79fbe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
build: (elements) => { | |
// デバイスタイプを取得(存在しない場合はmobile) | |
const device = process.env.DEVICE || 'mobile'; | |
// 整形すべきものはobjectでなくてはならない | |
if (typeof elements !== 'object') { | |
throw new Error('Invalid Argument'); | |
} | |
const new_object = {}; | |
for (var key in elements) { | |
new_object[key] = (element => { | |
switch (typeof element) { | |
case 'string': { | |
// 文字列の場合はmobile/tablet共通でselectorを省略している | |
return {selector: element}; | |
} | |
case 'object': { | |
// mobile/tablet共通の設定 | |
if ('selector' in element) { | |
return element; | |
} | |
// デバイス毎の設定を確認 | |
if (device in element) { | |
return (element => { | |
// selectorが省略可能なので判定して整形 | |
switch (typeof element) { | |
case 'string': { | |
return {selector: element}; | |
} | |
default: { | |
// ここではobjectかどうかは判定しない | |
return element; | |
} | |
} | |
})(element[device]); | |
} | |
// 設定が存在しない | |
return null; | |
} | |
default: { | |
// object/string以外は不正なので例外 | |
throw new Error('Invalid Structure'); | |
} | |
} | |
})(elements[key]); | |
} | |
return new_object; | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment