Skip to content

Commit

Permalink
build(deps-dev): bump @ember/string from 3.1.1 to 4.0.0 (#394)
Browse files Browse the repository at this point in the history
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: MrChocolatine <[email protected]>
  • Loading branch information
dependabot[bot] and MrChocolatine authored Aug 17, 2024
1 parent 674aa44 commit 277d2b8
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 123 deletions.
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
2 changes: 1 addition & 1 deletion tests/unit/initializers/export-application-global-test.ts
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

0 comments on commit 277d2b8

Please sign in to comment.