-
Notifications
You must be signed in to change notification settings - Fork 156
improv(commons): expand type utils functions #2189
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
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0f445ca
improv(commons): expand type utils functions
dreamorosi e2f68c3
Merge branch 'main' into improv/commons/typeutils
dreamorosi 969a79c
Update packages/commons/tests/unit/typeUtils.test.ts
dreamorosi fd20ee1
Merge branch 'main' into improv/commons/typeutils
am29d ab890f3
chore: add number as record key type
dreamorosi 18a86e0
Merge branch 'main' into improv/commons/typeutils
dreamorosi df0b923
Merge branch 'main' into improv/commons/typeutils
dreamorosi 8b95d3c
Merge branch 'main' into improv/commons/typeutils
dreamorosi 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
This file was deleted.
Oops, something went wrong.
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,177 @@ | ||
/** | ||
* Returns true if the passed value is a record (object). | ||
* | ||
* @param value The value to check | ||
*/ | ||
const isRecord = (value: unknown): value is Record<string, unknown> => { | ||
return ( | ||
Object.prototype.toString.call(value) === '[object Object]' && | ||
!Object.is(value, null) | ||
); | ||
}; | ||
|
||
/** | ||
* Check if a value is a string. | ||
* | ||
* @param value The value to check | ||
*/ | ||
const isString = (value: unknown): value is string => { | ||
return typeof value === 'string'; | ||
}; | ||
|
||
/** | ||
* Check if a value is a number. | ||
* | ||
* @param value The value to check | ||
*/ | ||
const isNumber = (value: unknown): value is number => { | ||
return typeof value === 'number'; | ||
}; | ||
|
||
/** | ||
* Check if a value is an integer number. | ||
* | ||
* @param value The value to check | ||
*/ | ||
const isIntegerNumber = (value: unknown): value is number => { | ||
return isNumber(value) && Number.isInteger(value); | ||
}; | ||
|
||
/** | ||
* Check if a value is truthy. | ||
* | ||
* @param value The value to check | ||
*/ | ||
const isTruthy = (value: unknown): boolean => { | ||
am29d marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (isString(value)) { | ||
return value !== ''; | ||
} else if (isNumber(value)) { | ||
return value !== 0; | ||
} else if (typeof value === 'boolean') { | ||
return value; | ||
} else if (Array.isArray(value)) { | ||
return value.length > 0; | ||
} else if (isRecord(value)) { | ||
return Object.keys(value).length > 0; | ||
} else { | ||
return false; | ||
} | ||
}; | ||
|
||
/** | ||
* Check if a value is null. | ||
* | ||
* @param value The value to check | ||
*/ | ||
const isNull = (value: unknown): value is null => { | ||
return Object.is(value, null); | ||
am29d marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
/** | ||
* Check if a value is null or undefined. | ||
* | ||
* @param value The value to check | ||
*/ | ||
const isNullOrUndefined = (value: unknown): value is null | undefined => { | ||
return isNull(value) || Object.is(value, undefined); | ||
}; | ||
|
||
/** | ||
* Get the type of a value as a string. | ||
* | ||
* @param value The value to check | ||
*/ | ||
const getType = (value: unknown): string => { | ||
if (Array.isArray(value)) { | ||
return 'array'; | ||
} else if (isRecord(value)) { | ||
return 'object'; | ||
} else if (isString(value)) { | ||
return 'string'; | ||
} else if (isNumber(value)) { | ||
return 'number'; | ||
} else if (typeof value === 'boolean') { | ||
return 'boolean'; | ||
} else if (isNull(value)) { | ||
return 'null'; | ||
} else { | ||
return 'unknown'; | ||
} | ||
}; | ||
|
||
/** | ||
* Compare two arrays for strict equality. | ||
* | ||
* @param left The left array to compare | ||
* @param right The right array to compare | ||
*/ | ||
const areArraysEqual = (left: unknown[], right: unknown[]): boolean => { | ||
if (left.length !== right.length) { | ||
return false; | ||
} | ||
|
||
return left.every((value, i) => isStrictEqual(value, right[i])); | ||
}; | ||
|
||
/** | ||
* Compare two records for strict equality. | ||
* | ||
* @param left The left record to compare | ||
* @param right The right record to compare | ||
*/ | ||
const areRecordsEqual = ( | ||
left: Record<string, unknown>, | ||
right: Record<string, unknown> | ||
): boolean => { | ||
const leftKeys = Object.keys(left); | ||
const rightKeys = Object.keys(right); | ||
|
||
if (leftKeys.length !== rightKeys.length) { | ||
return false; | ||
} | ||
|
||
return leftKeys.every((key) => isStrictEqual(left[key], right[key])); | ||
}; | ||
|
||
/** | ||
* Check if two unknown values are strictly equal. | ||
* | ||
* If the values are arrays, then each element is compared, regardless of | ||
* order. If the values are objects, then each key and value from left | ||
* is compared to the corresponding key and value from right. If the | ||
* values are primitives, then they are compared using strict equality. | ||
* | ||
* @param left Left side of strict equality comparison | ||
* @param right Right side of strict equality comparison | ||
*/ | ||
const isStrictEqual = (left: unknown, right: unknown): boolean => { | ||
if (left === right) { | ||
return true; | ||
} | ||
|
||
if (typeof left !== typeof right) { | ||
return false; | ||
} | ||
|
||
if (Array.isArray(left) && Array.isArray(right)) { | ||
return areArraysEqual(left, right); | ||
} | ||
|
||
if (isRecord(left) && isRecord(right)) { | ||
return areRecordsEqual(left, right); | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
export { | ||
isRecord, | ||
isString, | ||
isNumber, | ||
isIntegerNumber, | ||
isTruthy, | ||
isNull, | ||
isNullOrUndefined, | ||
getType, | ||
isStrictEqual, | ||
}; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.