Skip to content

Instantly share code, notes, and snippets.

@dfkaye
Last active August 29, 2015 13:57
Show Gist options
  • Save dfkaye/9437273 to your computer and use it in GitHub Desktop.
Save dfkaye/9437273 to your computer and use it in GitHub Desktop.
camelCase path-names to symbols: example-name => exampleName; path.ext => path
var RE_SEP = /\-|\./;
var RE_SEP_AZ = /(\-|\.)[a-z]/;
var RE_SEP_AZ_G = /(\-|\.)[a-z]/g;
var RE_WS = /[\s]+/g;
var BLANK = '';
function camelize(path) {
var id = path.split('/').slice(-1)[0];
var m, c, i;
if (m = id.match(RE_SEP_AZ_G)) {
for (i = 0; i < m.length; i++) {
c = m[i].replace(RE_SEP, BLANK).toUpperCase();
id = id.replace(RE_SEP_AZ, c);
}
}
return (id.substring(0, id.lastIndexOf('.')) || id).replace(RE_WS, BLANK);
}
function test(path) {
// path vs fullpath (with ext)
// map symbol to path
var symbol = camelize(path);
console.log(path + ' => "' + symbol + '"');
// if argument at x is string, do the symbol part
// if argument is config object, TODO...
}
test('path/to/file'); // => file
test('path/to/some-module'); // => someModule
test('path/to/some-other-module'); // => someOtherModule
test('path/to/some-file.js'); // => someFileJs
test('path/to/dot.name'); // => dotName
test('path/to/file.with.extension'); // => fileWithExtension
test('path/to/under_score'); // need a decision: underScore or under_score?
// errors
test('path/to/ WHITE SPACE '); // = 'WHITESPACE'
test('path/to/this'); // = this
test('path/to/undefined'); // => undefined
test('path/to/null'); // => null
test('path/to/""'); // => ""
test('path/to/0'); // => 0
test('path/to/-1'); // => -1
test('path/to/true'); // => true
test('path/to/false'); // => false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment