-
Notifications
You must be signed in to change notification settings - Fork 2
Add kebabToCamelCase and mapObjectKeys utilities.
#113
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b089b9d
Add `kebabToCamelCase` and `mapObjectKeys` utilities.
Mati365 a969a0f
Fix incorrect file names
Mati365 7be613f
Minor test fixes.
Mati365 832b763
Simplify method.
Mati365 bf3bb89
Adjust changelog.
Mati365 b0d2548
Adjust comment.
Mati365 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| type: Feature | ||
| --- | ||
|
|
||
| Added `mapObjectKeys` helper for transforming object keys and `kebabToCamelCase` for converting strings from kebab-case to camelCase. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /** | ||
| * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved. | ||
| * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options | ||
| */ | ||
|
|
||
| /** | ||
| * Converts a kebab-case string to camelCase. | ||
| * @param str - The kebab-case string (e.g., "font-size"). | ||
| * @returns The camelCase string (e.g., "fontSize"). | ||
| */ | ||
| export function kebabToCamelCase( str: string ): string { | ||
| return str.replace( /-([a-z])/g, ( match: string, letter: string ) => letter.toUpperCase() ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /** | ||
| * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved. | ||
| * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options | ||
| */ | ||
|
|
||
| /** | ||
| * Creates a new object with keys mapped using the provided function. | ||
| * | ||
| * @param obj - The input object. | ||
| * @param fn - The mapping function (takes the original key, returns the new one). | ||
| * If produces the same key for multiple inputs, later entries override earlier ones. | ||
| * @returns A new object with the mapped keys. | ||
| */ | ||
| export function mapObjectKeys<T>( | ||
|
Mati365 marked this conversation as resolved.
|
||
| obj: Record<string, T>, | ||
| fn: ( key: string ) => string | ||
| ): Record<string, T> { | ||
| return Object.fromEntries( | ||
| Object.entries( obj ).map( ( [ key, value ] ) => [ fn( key ), value ] ) | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /** | ||
| * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved. | ||
| * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options | ||
| */ | ||
|
|
||
| import { describe, it, expect } from 'vitest'; | ||
| import { kebabToCamelCase } from '../../src/utils/kebabToCamelCase.js'; | ||
|
|
||
| describe( 'kebabToCamelCase', () => { | ||
| it( 'should convert "font-size" to "fontSize"', () => { | ||
| expect( kebabToCamelCase( 'font-size' ) ).toBe( 'fontSize' ); | ||
| } ); | ||
|
|
||
| it( 'should convert properties with multiple dashes', () => { | ||
| expect( kebabToCamelCase( 'border-bottom-width' ) ).toBe( 'borderBottomWidth' ); | ||
| } ); | ||
|
|
||
| it( 'should not change words without dashes', () => { | ||
| expect( kebabToCamelCase( 'color' ) ).toBe( 'color' ); | ||
| expect( kebabToCamelCase( 'margin' ) ).toBe( 'margin' ); | ||
| } ); | ||
| } ); | ||
|
cursor[bot] marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /** | ||
| * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved. | ||
| * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options | ||
| */ | ||
|
|
||
| import { describe, it, expect } from 'vitest'; | ||
| import { mapObjectKeys } from '../../src/utils/mapObjectKeys.js'; | ||
|
|
||
| describe( 'mapObjectKeys', () => { | ||
| it( 'should correctly map keys using a simple uppercase function', () => { | ||
| const obj: Record<string, number> = { a: 1, b: 2 }; | ||
| const result = mapObjectKeys( obj, key => key.toUpperCase() ); | ||
|
|
||
| expect( result ).toEqual( { A: 1, B: 2 } ); | ||
| } ); | ||
|
|
||
| it( 'should add a prefix to all keys in the object', () => { | ||
| const data: Record<string, string> = { | ||
| name: 'Alice', | ||
| role: 'Admin' | ||
| }; | ||
|
|
||
| const prefixedData = mapObjectKeys( data, key => `user_${ key }` ); | ||
|
|
||
| expect( prefixedData ).toEqual( { | ||
| user_name: 'Alice', | ||
| user_role: 'Admin' | ||
| } ); | ||
| } ); | ||
|
|
||
| it( 'should not mutate the original object', () => { | ||
| const obj: Record<string, string> = { originalKey: 'value' }; | ||
|
|
||
| mapObjectKeys( obj, key => `new_${ key }` ); | ||
|
|
||
| expect( obj ).toEqual( { originalKey: 'value' } ); | ||
| } ); | ||
| } ); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.