-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './omit'; | ||
export * from './omitBy'; | ||
export * from './flatten'; | ||
export * from './unflatten'; |
This file contains 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,25 @@ | ||
import unflatten from './unflatten'; | ||
|
||
describe('unflatten', () => { | ||
it('unflatten the given object', () => { | ||
const flatObject = { | ||
'hello.yes': 'true', | ||
flat: 'yes', | ||
'nested.array': [1, 2], | ||
'furthernested.yes.no': false, | ||
}; | ||
|
||
const result = unflatten(flatObject, '.'); | ||
|
||
expect(result).toEqual({ | ||
hello: { | ||
yes: 'true', | ||
}, | ||
flat: 'yes', | ||
nested: { | ||
array: [1, 2], | ||
}, | ||
furthernested: { yes: { no: false } }, | ||
}); | ||
}); | ||
}); |
This file contains 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,33 @@ | ||
import type { NonEmptyArray } from '../types/arrays'; | ||
import type { Character } from '../types/strings'; | ||
|
||
function setNestedValue<Result extends object>( | ||
obj: Result, | ||
keys: NonEmptyArray<string>, | ||
value: unknown | ||
): Result { | ||
const [first, ...rest] = keys; | ||
if (rest.length === 0) return { ...obj, [first]: value }; | ||
|
||
const nestedObject = (obj as Record<string, unknown>)[first] ?? {}; | ||
|
||
return { | ||
...obj, | ||
[first]: setNestedValue(nestedObject, rest as NonEmptyArray<string>, value), | ||
}; | ||
} | ||
|
||
function unflatten<Result extends object, Delimiter extends string>( | ||
data: object, | ||
delimiter: Character<Delimiter> | ||
): Result { | ||
const initialResult = {} as unknown as Result; | ||
|
||
return Object.entries(data).reduce<Result>((acc, [key, value]) => { | ||
const keys = key.split(delimiter) as NonEmptyArray<string>; | ||
|
||
return setNestedValue(acc, keys, value); | ||
}, initialResult); | ||
} | ||
|
||
export default unflatten; |
This file contains 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 @@ | ||
export type NonEmptyArray<TargetArray> = [TargetArray, ...TargetArray[]]; |
This file contains 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,2 @@ | ||
export * from './strings'; | ||
export * from './arrays'; |
This file contains 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,7 @@ | ||
type OnlyFirstCharacter<TargetString extends string> = | ||
TargetString extends `${infer $TFirstChar}${string}` ? $TFirstChar : string; | ||
|
||
export type Character<TargetString extends string> = | ||
TargetString extends TargetString & OnlyFirstCharacter<TargetString> | ||
? TargetString & OnlyFirstCharacter<TargetString> | ||
: string & { length: 1 }; |