-
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.
feat(typescript): improve types and methods definitions
- Loading branch information
1 parent
4bc1060
commit c1e28c1
Showing
18 changed files
with
240 additions
and
104 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,25 @@ | ||
import getArrayIndex from './utils/get-array-index'; | ||
import getKey from './utils/get-key'; | ||
import shallowCopy from './utils/shallow-copy'; | ||
|
||
/** | ||
* Parse object key from dot notation | ||
* Parse an object key from dot notation | ||
* @example | ||
* parseKey('person.name', 'John Doe'); | ||
* // outputs { person: { name: 'John Doe' } } | ||
* // output { person: { name: 'John Doe' } } | ||
* parseKey('person.alias[]', 'John Doe'); | ||
* // outputs { person: { alias: ['John Doe] } } | ||
* @param {string} path - Dot notation object path | ||
* @param {any} value - Dot notation path value | ||
* // output { person: { alias: ['John Doe'] } } | ||
* @param {string} path - Dot notation path | ||
* @param {unknown} value | ||
* @returns {object} | ||
*/ | ||
const parseKey = <T>(path: string, value: unknown): T extends [] ? T[] : T => { | ||
const [current, remaining] = getKey(path); | ||
const match = getArrayIndex(current); | ||
const parseKey = <T>(path: string, value: unknown): T extends [] ? Array<T> : T => { | ||
const [key, remainingPath] = getKey(path); | ||
const hasArrayNotation = getArrayIndex(key); | ||
|
||
const mount = (): T => (remaining ? parseKey<T>(remaining, value) : value) as T; | ||
const compiledValue = (remainingPath ? parseKey<T>(remainingPath, value) : shallowCopy(value)) as T; | ||
|
||
return (match ? [mount()] : { [current]: mount() }) as T extends [] ? T[] : T; | ||
return (hasArrayNotation ? [compiledValue] : { [key]: compiledValue }) as T extends [] ? Array<T> : T; | ||
}; | ||
|
||
export default parseKey; |
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,48 +1,51 @@ | ||
import getArrayIndex from './utils/get-array-index'; | ||
import getKey from './utils/get-key'; | ||
import is from './utils/is'; | ||
import shallowCopy from './utils/shallow-copy'; | ||
|
||
/** | ||
* Pick | ||
* @template T, S | ||
* @description Reads value from object using dot notation path as key | ||
* Pick value at a given dot notation path | ||
* @template T | ||
* @param {Object.<string, unknown> | Object.<string, unknown[]} source | ||
* @param {string} path | ||
* @param {T} source | ||
* @returns {T} value | ||
*/ | ||
export const pick = <T = unknown, S = Record<string, unknown | unknown[]> | unknown[]>(source: S, path: string): T => { | ||
const pick = <T>(source: Record<string, unknown> | Array<Record<string, unknown>>, path: string): T => { | ||
if (is.nullOrUndefined(path) || !path.trim()) { | ||
throw new SyntaxError(`A dot notation path was expected, but instead got "${path}"`); | ||
} | ||
|
||
const content = shallowCopy(source) as Record<string, unknown>; | ||
|
||
// eslint-disable-next-line prefer-const | ||
let [current, remaining] = getKey(path) as [string | number, string | undefined]; | ||
let [key, remainingPath]: [string | number, string | undefined] = getKey(path); | ||
|
||
const match = getArrayIndex(current.toString()); | ||
const hasArrayNotation = getArrayIndex(key.toString()); | ||
|
||
if (match) { | ||
const { 1: index } = match; | ||
if (hasArrayNotation) { | ||
const { 1: idx } = hasArrayNotation; | ||
|
||
if (!index) { | ||
if (!idx) { | ||
throw new SyntaxError(`An array index was expected but nothing was found at "${path}"`); | ||
} | ||
|
||
if (Number.isNaN(+index)) { | ||
throw new TypeError(`Array index must a positive integer "${index}"`); | ||
if (Number.isNaN(+idx)) { | ||
throw new TypeError(`Array index must a positive integer "${idx}"`); | ||
} | ||
|
||
if (+index < 0) { | ||
throw new RangeError(`Array index must be equal or greater than 0, but instead got "${index}"`); | ||
if (+idx < 0) { | ||
throw new RangeError(`Array index must be equal or greater than 0, but instead got "${idx}"`); | ||
} | ||
|
||
current = +index; | ||
// replace key with array index value | ||
key = +idx; | ||
} | ||
|
||
if (!remaining || !(source as Record<string, T | unknown>)[current]) { | ||
return (source as Record<string, T | unknown>)[current] as T; | ||
if (!remainingPath || is.nullOrUndefined(content[key])) { | ||
return content[key] as T; | ||
} | ||
|
||
return pick<T, S>((source as Record<string, T | unknown>)[current] as S, remaining); | ||
return pick<T>(content[key] as Record<string, unknown>, remainingPath); | ||
}; | ||
|
||
export default pick; |
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,15 +1,15 @@ | ||
/** | ||
* Ger Array Index | ||
* @description get array index from given string | ||
* @description get array index from dot notation path string | ||
* @param {string} str | ||
* @return {string[]|null} | ||
* @return {RegExpExecArray|null} | ||
*/ | ||
const getArrayIndex = (str: string): RegExpExecArray | null => { | ||
return getArrayIndex.regexNaNIndex.exec(str) || getArrayIndex.regexIntegerIndex.exec(str); | ||
return getArrayIndex.regexpNaNIndex.exec(str) || getArrayIndex.regexpIntIndex.exec(str); | ||
}; | ||
|
||
getArrayIndex.regexIntegerIndex = /\[([-]*\d*)\]/g; | ||
getArrayIndex.regexpIntIndex = /\[(-*\d*)]/g; | ||
|
||
getArrayIndex.regexNaNIndex = /\[([^\]]*)\]/; | ||
getArrayIndex.regexpNaNIndex = /\[([^\]]*)]/; | ||
|
||
export default getArrayIndex; |
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
Oops, something went wrong.