Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build(deps-dev): bump @ember/string from 3.1.1 to 4.0.0 #394

Merged
merged 3 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion addon/initializers/export-application-global.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Application from '@ember/application'
import { classify } from '@ember/string'
import { classify } from 'ember-cli-embedded/utils/classify'

export function initialize(application: Application): void {
const env = application.resolveRegistration('config:environment') as {
Expand Down
46 changes: 46 additions & 0 deletions addon/utils/classify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* The content of this file was partially copied-pasted from:
* https://github.com/emberjs/ember-string/blob/v4.0.0-%40ember/string/src/index.ts
*/

const STRING_CLASSIFY_REGEXP_1 = /^(-|_)+(.)?/
const STRING_CLASSIFY_REGEXP_2 = /(.)(-|_|\.|\s)+(.)?/g
const STRING_CLASSIFY_REGEXP_3 = /(^|\/|\.)([a-z])/g

function CLASSIFY_CACHE(str: string) {
const replace1 = (_match: string, _separator: string, chr: string) =>
chr ? `_${chr.toUpperCase()}` : ''

const replace2 = (_match: string, initialChar: string, _separator: string, chr: string) =>
initialChar + (chr ? chr.toUpperCase() : '')

const parts = str.split('/')

for (let i = 0; i < parts.length; i++) {
parts[i] = (parts[i] ?? '')
.replace(STRING_CLASSIFY_REGEXP_1, replace1)
.replace(STRING_CLASSIFY_REGEXP_2, replace2)
}

return parts
.join('/')
.replace(STRING_CLASSIFY_REGEXP_3, (match /*, separator, chr */) => match.toUpperCase())
}

/**
* Returns the UpperCamelCase form of a string.
*
* ```javascript
* classify('innerHTML'); // 'InnerHTML'
* classify('action_name'); // 'ActionName'
* classify('css-class-name'); // 'CssClassName'
* classify('my favorite items'); // 'MyFavoriteItems'
* classify('private-docs/owner-invoice'); // 'PrivateDocs/OwnerInvoice'
* ```
*
* @param {string} str the string to classify
* @return {string} the classified string
*/
export function classify(str: string): string {
return CLASSIFY_CACHE(str)
}
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
},
"devDependencies": {
"@ember/optional-features": "^2.0.0",
"@ember/string": "^3.1.1",
"@ember/test-helpers": "^3.2.0",
"@embroider/test-setup": "^4.0.0",
"@glint/environment-ember-loose": "^1.2.1",
Expand Down Expand Up @@ -132,7 +131,6 @@
"webpack": "^5.89.0"
},
"peerDependencies": {
"@ember/string": "^3.1.1",
"ember-source": ">= 4.0.0"
},
"engines": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Application from '@ember/application'
import { initialize } from 'dummy/initializers/export-application-global'
import { module, test } from 'qunit'
import Resolver from 'ember-resolver'
import { classify } from '@ember/string'
import { classify } from 'ember-cli-embedded/utils/classify'

import type AppConfig from 'dummy/config/environment'

Expand Down
105 changes: 105 additions & 0 deletions tests/unit/utils/classify-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* The content of this file was extracted (then adapted) from:
* https://github.com/emberjs/ember-string/blob/v4.0.0-%40ember/string/tests/unit/classify_test.ts
*/

import { classify } from 'ember-cli-embedded/utils/classify'
import { module, test } from 'qunit'

module('Unit | Utility | classify', function () {
test('it works', function (assert) {
assert.strictEqual(
classify('my favorite items'),
'MyFavoriteItems',
'classify normal string',
/* */
)

assert.strictEqual(
classify('css-class-name'),
'CssClassName',
'classify dasherized string',
/* */
)

assert.strictEqual(
classify('action_name'),
'ActionName',
'classify underscored string',
/* */
)

assert.strictEqual(
classify('privateDocs/ownerInvoice'),
'PrivateDocs/OwnerInvoice',
'classify namespaced camelized string',
)

assert.strictEqual(
classify('private_docs/owner_invoice'),
'PrivateDocs/OwnerInvoice',
'classify namespaced underscored string',
)

assert.strictEqual(
classify('private-docs/owner-invoice'),
'PrivateDocs/OwnerInvoice',
'classify namespaced dasherized string',
)

assert.strictEqual(
classify('-view-registry'),
'_ViewRegistry',
'classify prefixed dasherized string',
)

assert.strictEqual(
classify('components/-text-field'),
'Components/_TextField',
'classify namespaced prefixed dasherized string',
)

assert.strictEqual(
classify('_Foo_Bar'),
'_FooBar',
'classify underscore-prefixed underscored string',
)

assert.strictEqual(
classify('_Foo-Bar'),
'_FooBar',
'classify underscore-prefixed dasherized string',
)

assert.strictEqual(
classify('_foo/_bar'),
'_Foo/_Bar',
'classify underscore-prefixed-namespaced underscore-prefixed string',
)

assert.strictEqual(
classify('-foo/_bar'),
'_Foo/_Bar',
'classify dash-prefixed-namespaced underscore-prefixed string',
)

assert.strictEqual(
classify('-foo/-bar'),
'_Foo/_Bar',
'classify dash-prefixed-namespaced dash-prefixed string',
)

assert.strictEqual(
/* */
classify('InnerHTML'),
'InnerHTML',
'does nothing with classified string',
)

assert.strictEqual(
classify('_FooBar'),
'_FooBar',
'does nothing with classified prefixed string',
)
})
})
Loading
Loading