const assert = require('assert');
|
const is = require('is-type-of');
|
|
exports.getProperties = (filepath, { caseStyle }) => {
|
// if caseStyle is function, return the result of function
|
if (is.function(caseStyle)) {
|
const result = caseStyle(filepath);
|
assert(is.array(result), `caseStyle expect an array, but got ${result}`);
|
return result;
|
}
|
// use default camelize
|
return this.defaultCamelize(filepath, caseStyle);
|
}
|
|
exports.defaultCamelize = (filepath, caseStyle) => {
|
const properties = filepath.substring(0, filepath.lastIndexOf('.')).split('/');
|
return properties.map(property => {
|
if (!/^[a-z][a-z0-9_-]*$/i.test(property)) {
|
throw new Error(`${property} is not match 'a-z0-9_-' in ${filepath}`);
|
}
|
|
property = property.replace(/[_-][a-z]/ig, s => s.substring(1).toUpperCase());
|
let first = property[0];
|
switch (caseStyle) {
|
case 'lower':
|
first = first.toLowerCase();
|
break;
|
case 'upper':
|
first = first.toUpperCase();
|
break;
|
case 'camel':
|
default:
|
}
|
return first + property.substring(1);
|
});
|
}
|