Skip to content

Commit

Permalink
Use Ember's component dasherize logic (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
patocallaghan authored Nov 19, 2020
1 parent 4f61a54 commit a8fbf6c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
34 changes: 15 additions & 19 deletions addon/utils/normalizers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,29 @@ export function normalizePath(path) {
return dasherizePath(path.split("\\").join("/"));
}

// Take from https://github.com/emberjs/ember.js/blob/b31998b6a0cccd22a8fb6fab21d24e5e7f2cb70d/packages/ember-template-compiler/lib/system/dasherize-component-name.ts
// we need this because Ember.String.dasherize('XTestWrapper') -> xtest-wrapper, not x-test-wrapper
const SIMPLE_DASHERIZE_REGEXP = /[A-Z]|::/g;
const ALPHA = /[A-Za-z0-9]/;
export function dasherizeName(name = '') {
const result = [];
const nameSize = name.length;
if (!nameSize) {
return '';
}
result.push(name.charAt(0));
for (let i = 1; i < nameSize; i++) {
let char = name.charAt(i);
if (char === char.toUpperCase()) {
if (char !== '-' && char !== '/' && char !== '_') {
if (result[result.length - 1] !== '-' && result[result.length - 1] !== '/') {
result.push('-');
}
}
}
result.push(char);
}
return result.join('');
return name.replace(SIMPLE_DASHERIZE_REGEXP, (char, index) => {
if (char === '::') {
return '/';
}

if (index === 0 || !ALPHA.test(name[index - 1])) {
return char.toLowerCase();
}

return `-${char.toLowerCase()}`;
})
}

export function normalizeComponentName(name) {
if (typeof name !== 'string') {
return name;
} else {
return dasherizeName(name).toLowerCase();
return dasherizeName(name);
}
}

Expand Down
9 changes: 5 additions & 4 deletions tests/unit/utils/normalizers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module('Unit | Utility | normalizers', function() {
assert.equal(componentNameFromClassName('FooBarClass'), 'foo-bar');
assert.equal(componentNameFromClassName('FooBarComponent'), 'foo-bar');
assert.equal(componentNameFromClassName('XFooBarClassComponent'), 'x-foo-bar');
assert.equal(componentNameFromClassName('FooV2'), 'foo-v2');
});

test('dasherizePath should dasherize camelized paths', function(assert){
Expand All @@ -27,10 +28,10 @@ module('Unit | Utility | normalizers', function() {
});

test('dasherizeName should convert component names to valid form', function(assert) {
assert.equal(dasherizeName('XFooBar'), 'X-Foo-Bar');
assert.equal(dasherizeName('XFooBar/BooBaz'), 'X-Foo-Bar/Boo-Baz');
assert.equal(dasherizeName('FooBar/BooBaz'), 'Foo-Bar/Boo-Baz');
assert.equal(dasherizeName('Foo/Boo'), 'Foo/Boo');
assert.equal(dasherizeName('XFooBar'), 'x-foo-bar');
assert.equal(dasherizeName('XFooBar/BooBaz'), 'x-foo-bar/boo-baz');
assert.equal(dasherizeName('FooBar/BooBaz'), 'foo-bar/boo-baz');
assert.equal(dasherizeName('Foo/Boo'), 'foo/boo');
});

test('normalizeComponentName should convert component name to valid lowercased form', function(assert){
Expand Down

0 comments on commit a8fbf6c

Please sign in to comment.