Skip to content

Commit

Permalink
Narrowed usage to entity names in @codemod-utils/ember (#130)
Browse files Browse the repository at this point in the history
* refactor: Reorganized files

* breaking: Removed camelize() (unused)

* chore: Updated tests to document correct usage

* refactor: Simplified classify() and renamed it to pascalize()

* refactor: Simplified doubleColonize()

* feature: Created camelize()

* refactor: Simplified pascalize()

* chore: Simplified README

* chore: Added changeset

---------

Co-authored-by: ijlee2 <ijlee2@users.noreply.github.com>
  • Loading branch information
ijlee2 and ijlee2 authored Aug 28, 2024
1 parent f64c944 commit cff4295
Show file tree
Hide file tree
Showing 30 changed files with 254 additions and 231 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-donkeys-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@codemod-utils/ember": major
---

Narrowed usage to entity names, in order to remove dependency on ember-cli-string-utils
31 changes: 13 additions & 18 deletions packages/ember/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,42 @@ _Utilities for Ember_

## API

The methods are built with the **entity name** in mind, a string (possibly with hyphens and forward slashes) that represents a set of related files in Ember.

(For example, the `<Ui::Button>` component has the entity name `'ui/button'`. In the Ember source code, you may see variable names like `entityName` or `moduleName`.)


### camelize

Returns a string in camel-case.
Converts an entity name to camel case. Used for naming the function that is associated with the entity.

```ts
import { camelize } from '@codemod-utils/ember';

const newValue = camelize('css-class-name');
const newValue = camelize('ui/form/generate-error-message');

// 'cssClassName'
// 'uiFormGenerateErrorMessage'
```


### classify
### doubleColonize

Returns a string that can be used to name a JavaScript `class` (a.k.a. Pascal case).
Converts an entity name to double colon (`::`) case. Used for writing the angle bracket syntax or the signature for a component.

```ts
import { classify } from '@codemod-utils/ember';
import { doubleColonize } from '@codemod-utils/ember';

const newValue = classify('ui/button');
const newValue = doubleColonize('ui/form/input');

// 'UiButton'
// 'Ui::Form::Input'
```


### doubleColonize
### pascalize

Returns a string associated with the angle bracket syntax for components.
Converts an entity name to Pascal case. Used for naming the class that is associated with the entity.

```ts
import { doubleColonize } from '@codemod-utils/ember';
import { pascalize } from '@codemod-utils/ember';

const newValue = doubleColonize('ui/button');
const newValue = pascalize('ui/form/input');

// 'Ui::Button'
// 'UiFormInput'
```


Expand Down
28 changes: 28 additions & 0 deletions packages/ember/src/ember/entity-name/camelize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { pascalize } from './pascalize.js';

/**
* Converts an entity name to camel case. Used for naming the
* function that is associated with the entity.
*
* @param entityName
*
* The name of an entity (made up of lowercase letters, hyphen,
* and forward slash).
*
* @return
*
* The name in camel case.
*
* @example
*
* ```ts
* const newValue = camelize('ui/form/generate-error-message');
*
* // 'uiFormGenerateErrorMessage'
* ```
*/
export function camelize(entityName: string): string {
const pascalizedName = pascalize(entityName);

return pascalizedName.charAt(0).toLowerCase() + pascalizedName.substring(1);
}
33 changes: 33 additions & 0 deletions packages/ember/src/ember/entity-name/double-colonize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function pascalize(value: string): string {
return value
.split('-')
.map((token) => {
return token.charAt(0).toUpperCase() + token.substring(1).toLowerCase();
})
.join('');
}

/**
* Converts an entity name to double colon (`::`) case. Used for
* writing the angle bracket syntax or the signature for a component.
*
* @param entityName
*
* The name of an entity (made up of lowercase letters, hyphen,
* and forward slash).
*
* @return
*
* The name in double colon case.
*
* @example
*
* ```ts
* const newValue = doubleColonize('ui/form/input');
*
* // 'Ui::Form::Input'
* ```
*/
export function doubleColonize(entityName: string): string {
return entityName.split('/').map(pascalize).join('::');
}
28 changes: 28 additions & 0 deletions packages/ember/src/ember/entity-name/pascalize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { doubleColonize } from './double-colonize.js';

/**
* Converts an entity name to Pascal case. Used for naming the
* class that is associated with the entity.
*
* @param entityName
*
* The name of an entity (made up of lowercase letters, hyphen,
* and forward slash).
*
* @return
*
* The name in Pascal case.
*
* @example
*
* ```ts
* const newValue = pascalize('ui/form/input');
*
* // 'UiFormInput'
* ```
*/
export function pascalize(entityName: string): string {
const doubleColonizedName = doubleColonize(entityName);

return doubleColonizedName.replaceAll('::', '');
}
34 changes: 0 additions & 34 deletions packages/ember/src/entity-name/camelize.ts

This file was deleted.

35 changes: 0 additions & 35 deletions packages/ember/src/entity-name/classify.ts

This file was deleted.

30 changes: 0 additions & 30 deletions packages/ember/src/entity-name/double-colonize.ts

This file was deleted.

6 changes: 3 additions & 3 deletions packages/ember/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './entity-name/camelize.js';
export * from './entity-name/classify.js';
export * from './entity-name/double-colonize.js';
export * from './ember/entity-name/camelize.js';
export * from './ember/entity-name/double-colonize.js';
export * from './ember/entity-name/pascalize.js';
11 changes: 11 additions & 0 deletions packages/ember/tests/ember/entity-name/camelize/base-case.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { assert, test } from '@codemod-utils/tests';

import { camelize } from '../../../../src/index.js';

test('utils | ember | entity-name | camelize > base case', function () {
assert.strictEqual(camelize('hello'), 'hello');

assert.strictEqual(camelize('hello-world'), 'helloWorld');

assert.strictEqual(camelize('hello-world-123'), 'helloWorld123');
});
16 changes: 16 additions & 0 deletions packages/ember/tests/ember/entity-name/camelize/nested.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { assert, test } from '@codemod-utils/tests';

import { camelize } from '../../../../src/index.js';

test('utils | ember | entity-name | camelize > nested', function () {
assert.strictEqual(camelize('ui/form'), 'uiForm');

assert.strictEqual(camelize('ui/form/input'), 'uiFormInput');

assert.strictEqual(camelize('ui/form/submit-button'), 'uiFormSubmitButton');

assert.strictEqual(
camelize('widgets/widget-3/tour-schedule/responsive-image'),
'widgetsWidget3TourScheduleResponsiveImage',
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { assert, test } from '@codemod-utils/tests';

import { camelize } from '../../../../src/index.js';

test('utils | ember | entity-name | camelize > wrong input', function () {
assert.strictEqual(camelize(''), '');

assert.strictEqual(camelize('-'), '');

assert.strictEqual(camelize('/'), '');

assert.strictEqual(camelize('ui/'), 'ui');

assert.strictEqual(camelize('/ui'), 'ui');

assert.strictEqual(camelize('ui.form.input'), 'ui.form.input');

assert.strictEqual(camelize('ui_form_input'), 'ui_form_input');

assert.strictEqual(camelize('ui form input'), 'ui form input');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { assert, test } from '@codemod-utils/tests';

import { doubleColonize } from '../../../../src/index.js';

test('utils | ember | entity-name | double-colonize > base case', function () {
assert.strictEqual(doubleColonize('hello'), 'Hello');

assert.strictEqual(doubleColonize('hello-world'), 'HelloWorld');

assert.strictEqual(doubleColonize('hello-world-123'), 'HelloWorld123');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { assert, test } from '@codemod-utils/tests';

import { doubleColonize } from '../../../../src/index.js';

test('utils | ember | entity-name | double-colonize > nested', function () {
assert.strictEqual(doubleColonize('ui/form'), 'Ui::Form');
assert.strictEqual(doubleColonize('ui/form/input'), 'Ui::Form::Input');
assert.strictEqual(
doubleColonize('ui/form/submit-button'),
'Ui::Form::SubmitButton',
);
assert.strictEqual(
doubleColonize('widgets/widget-3/tour-schedule/responsive-image'),
'Widgets::Widget3::TourSchedule::ResponsiveImage',
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { assert, test } from '@codemod-utils/tests';

import { doubleColonize } from '../../../../src/index.js';

test('utils | ember | entity-name | double-colonize > wrong input', function () {
assert.strictEqual(doubleColonize(''), '');

assert.strictEqual(doubleColonize('-'), '');

assert.strictEqual(doubleColonize('/'), '::');

assert.strictEqual(doubleColonize('ui/'), 'Ui::');

assert.strictEqual(doubleColonize('/ui'), '::Ui');

assert.strictEqual(doubleColonize('ui.form.input'), 'Ui.form.input');

assert.strictEqual(doubleColonize('ui_form_input'), 'Ui_form_input');

assert.strictEqual(doubleColonize('ui form input'), 'Ui form input');
});
11 changes: 11 additions & 0 deletions packages/ember/tests/ember/entity-name/pascalize/base-case.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { assert, test } from '@codemod-utils/tests';

import { pascalize } from '../../../../src/index.js';

test('utils | ember | entity-name | pascalize > base case', function () {
assert.strictEqual(pascalize('hello'), 'Hello');

assert.strictEqual(pascalize('hello-world'), 'HelloWorld');

assert.strictEqual(pascalize('hello-world-123'), 'HelloWorld123');
});
16 changes: 16 additions & 0 deletions packages/ember/tests/ember/entity-name/pascalize/nested.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { assert, test } from '@codemod-utils/tests';

import { pascalize } from '../../../../src/index.js';

test('utils | ember | entity-name | pascalize > nested', function () {
assert.strictEqual(pascalize('ui/form'), 'UiForm');

assert.strictEqual(pascalize('ui/form/input'), 'UiFormInput');

assert.strictEqual(pascalize('ui/form/submit-button'), 'UiFormSubmitButton');

assert.strictEqual(
pascalize('widgets/widget-3/tour-schedule/responsive-image'),
'WidgetsWidget3TourScheduleResponsiveImage',
);
});
Loading

0 comments on commit cff4295

Please sign in to comment.