-
-
Notifications
You must be signed in to change notification settings - Fork 0
Added has(), set(), and assertPathExists()
#200
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 all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a665d98
feat: added `ParseNumber`
webdeveric a0bf74f
feat: added `FromPath`, `WithPath`, `Merge`
webdeveric bf286c8
feat: added `assertPathExists()`
webdeveric 4c071d8
feat: added `AllPaths`
webdeveric 7c7c4ed
feat: added `has()`
webdeveric ed505c3
feat: added `set()`
webdeveric 2d7d4fe
chore(get): updated `InputPath` type parameter
webdeveric 2d77d3c
chore: don't stringify `path`
webdeveric 71d8ad6
chore: updated test description
webdeveric 0aaf9b2
chore(deps): updated dependencies
webdeveric 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
Large diffs are not rendered by default.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { get } from '../get.js'; | ||
| import { has } from '../has.js'; | ||
|
|
||
| import type { TypePredicateFn } from '../types/functions.js'; | ||
| import type { WithPath } from '../types/objects.js'; | ||
|
|
||
| export function assertPathExists<Input extends object, InputPath extends string | number>( | ||
| input: Input, | ||
| path: InputPath, | ||
| ): asserts input is Input & WithPath<Input, InputPath>; | ||
|
|
||
| export function assertPathExists<Input extends object, InputPath extends string | number, Value>( | ||
| input: Input, | ||
| path: InputPath, | ||
| predicate: TypePredicateFn<Value>, | ||
| ): asserts input is Input & WithPath<Input, InputPath, Value>; | ||
|
|
||
| export function assertPathExists<Input extends object, InputPath extends string | number>( | ||
| input: Input, | ||
| path: InputPath, | ||
| predicate?: TypePredicateFn<unknown>, | ||
| ): asserts input is Input & WithPath<Input, InputPath> { | ||
| if (!has(input, path)) { | ||
| throw new Error(`object path (${path}) does not exist on input`); | ||
| } | ||
|
|
||
| if (predicate && !predicate(get(input, path))) { | ||
| throw new Error(`object path (${path}) failed predicate check`); | ||
| } | ||
| } |
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
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,49 @@ | ||
| import { pathParts } from './pathParts.js'; | ||
| import { isAnyObject } from './predicate/isAnyObject.js'; | ||
|
|
||
| import type { FromPath, Merge, Path, PathValue } from './types/objects.js'; | ||
| import type { IfNever, Pretty } from './types/utils.js'; | ||
|
|
||
| /** | ||
| * Determine if the path exists on an object. | ||
| */ | ||
| // Known path so the `Input` type is unchanged | ||
| export function has<Input extends object, InputPath extends Path<Input>>(input: Input, path: InputPath): input is Input; | ||
|
|
||
| // Unknown path so the `Input` type is extended | ||
| export function has<Input extends object, InputPath extends string | number>( | ||
| input: Input, | ||
| path: InputPath, | ||
| ): input is Pretty<Input & Merge<Input, FromPath<InputPath, unknown>>>; | ||
|
|
||
| // Unknown input can be assume to match at least the shape the input path makes | ||
| export function has<InputPath extends string | number>( | ||
| input: unknown, | ||
| path: InputPath, | ||
| ): input is FromPath<InputPath, unknown>; | ||
|
|
||
| export function has<Input extends object, InputPath extends Path<Input> | string | number>( | ||
| input: Input, | ||
| path: InputPath, | ||
| ): input is Input & FromPath<InputPath, IfNever<PathValue<Input, InputPath>, unknown, PathValue<Input, InputPath>>> { | ||
| if (!isAnyObject(input)) { | ||
| return false; | ||
| } | ||
|
|
||
| if (path === '') { | ||
| return false; | ||
| } | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| let current: any = input; | ||
|
|
||
| for (const part of pathParts(path)) { | ||
| if (typeof current === 'object' && current !== null && part in current) { | ||
| current = current[part]; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
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,58 @@ | ||
| import { pathParts } from './pathParts.js'; | ||
|
|
||
| import type { Path, PathValue } from './types/objects.js'; | ||
|
|
||
| /** | ||
| * Set the value of a property | ||
| */ | ||
| export function set<Input extends object, InputPath extends Path<Input>, Value extends PathValue<Input, InputPath>>( | ||
| input: Input, | ||
| path: InputPath, | ||
| value: Value, | ||
| ): Value; | ||
|
|
||
| export function set<Input extends object, InputPath extends string, Value>( | ||
| input: Input, | ||
| path: InputPath, | ||
| value: Value, | ||
| ): Value; | ||
|
|
||
| export function set<Input extends object, InputPath extends Path<Input> | string, Value>( | ||
| input: Input, | ||
| path: InputPath, | ||
| value: Value, | ||
| ): Value { | ||
| if (path === '') { | ||
| throw new Error('Path cannot be an empty string'); | ||
| } | ||
|
|
||
| if (typeof path === 'string' && /\b(__proto__|constructor|prototype)\b/.test(path)) { | ||
| throw new Error('Cannot pollute prototype'); | ||
webdeveric marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| const parts = Array.from(pathParts(path)); | ||
|
|
||
| const lastPart = parts.pop(); | ||
|
|
||
| if (typeof lastPart === 'undefined') { | ||
| throw new Error('Path must have at least one part'); | ||
| } | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| let current: any = input; | ||
webdeveric marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const useParts: PropertyKey[] = []; | ||
|
|
||
| // get a reference to the last node that will be updated | ||
| for (const part of parts) { | ||
| useParts.push(part); | ||
|
|
||
| if (typeof current === 'object' && current !== null && part in current) { | ||
| current = current[part]; | ||
| } else { | ||
| throw new Error(`Path "${useParts.join('.')}" does not exist in the input object`); | ||
| } | ||
| } | ||
|
|
||
| return (current[lastPart] = 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
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,27 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
|
|
||
| import { assertPathExists } from '../../src/assertion/assertPathExists.js'; | ||
| import { isNumber } from '../../src/predicate/isNumber.js'; | ||
| import { isString } from '../../src/predicate/isString.js'; | ||
|
|
||
| describe('assertPathExists()', () => { | ||
| it('Throws when path does not exist on object', () => { | ||
| expect(() => { | ||
| assertPathExists({ name: 'Test' }, 'name'); | ||
| }).not.toThrowError(); | ||
|
|
||
| expect(() => { | ||
| assertPathExists({ name: 'Test' }, 'age'); | ||
| }).toThrowError(); | ||
| }); | ||
|
|
||
| it('Throws when path does not pass predicate validation', () => { | ||
| expect(() => { | ||
| assertPathExists({ name: 'Test' }, 'name', isString); | ||
| }).not.toThrowError(); | ||
|
|
||
| expect(() => { | ||
| assertPathExists({ name: 'Test' }, 'name', isNumber); | ||
| }).toThrowError(); | ||
| }); | ||
| }); |
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,33 @@ | ||
| import { describe, it, expectTypeOf } from 'vitest'; | ||
|
|
||
| import { has } from '../src/has.js'; | ||
|
|
||
| describe('has()', () => { | ||
| describe('Is a type predicate', () => { | ||
| it('Handles unknown input', () => { | ||
| const input: unknown = { | ||
| firstName: 'Test', | ||
| }; | ||
|
|
||
| if (has(input, 'firstName')) { | ||
| expectTypeOf<typeof input>().toEqualTypeOf<{ firstName: unknown }>(); | ||
| } | ||
| }); | ||
|
|
||
| it('Handles tuple input', () => { | ||
| const data = ['test'] as const satisfies string[]; | ||
|
|
||
| if (has(data, 0)) { | ||
| expectTypeOf<typeof data>().toEqualTypeOf<['test']>(); | ||
| } | ||
| }); | ||
|
|
||
| it('Handles array input', () => { | ||
| const data: string[] = ['test']; | ||
|
|
||
| if (has(data, 0)) { | ||
| expectTypeOf<typeof data>().toEqualTypeOf<string[]>(); | ||
| } | ||
| }); | ||
| }); | ||
| }); |
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.