Skip to content

Commit

Permalink
Patch bugs (#4)
Browse files Browse the repository at this point in the history
* Fix copy paste errors

* Reorder unset definitions
  • Loading branch information
Siegrift authored Jun 4, 2019
1 parent c14e1fd commit fa91958
Show file tree
Hide file tree
Showing 13 changed files with 262 additions and 141 deletions.
183 changes: 105 additions & 78 deletions docs/globals.html

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ <h2 id="limitations">Limitations</h2>
<li class=" tsd-kind-function">
<a href="globals.html#isnullorundefined" class="tsd-kind-icon">is<wbr>Null<wbr>OrUndefined</a>
</li>
<li class=" tsd-kind-function">
<a href="globals.html#isobject" class="tsd-kind-icon">is<wbr>Object</a>
</li>
<li class=" tsd-kind-function tsd-has-type-parameter">
<a href="globals.html#map" class="tsd-kind-icon">map</a>
</li>
Expand Down
3 changes: 3 additions & 0 deletions docs/interfaces/dictionary.html
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ <h3 class="tsd-before-signature">Indexable</h3>
<li class=" tsd-kind-function">
<a href="../globals.html#isnullorundefined" class="tsd-kind-icon">is<wbr>Null<wbr>OrUndefined</a>
</li>
<li class=" tsd-kind-function">
<a href="../globals.html#isobject" class="tsd-kind-icon">is<wbr>Object</a>
</li>
<li class=" tsd-kind-function tsd-has-type-parameter">
<a href="../globals.html#map" class="tsd-kind-icon">map</a>
</li>
Expand Down
3 changes: 3 additions & 0 deletions docs/interfaces/numericdictionary.html
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ <h3 class="tsd-before-signature">Indexable</h3>
<li class=" tsd-kind-function">
<a href="../globals.html#isnullorundefined" class="tsd-kind-icon">is<wbr>Null<wbr>OrUndefined</a>
</li>
<li class=" tsd-kind-function">
<a href="../globals.html#isobject" class="tsd-kind-icon">is<wbr>Object</a>
</li>
<li class=" tsd-kind-function tsd-has-type-parameter">
<a href="../globals.html#map" class="tsd-kind-icon">map</a>
</li>
Expand Down
3 changes: 2 additions & 1 deletion src/lib/exist.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Optional, U } from '../types'
import { isNullOrUndefined } from 'util'
import { isObject } from '../utils'

/**
* Checks whether path exist in source value.
Expand Down Expand Up @@ -101,7 +102,7 @@ export function exist(source: any, path: any[]) {
while (++index < path.length) {
if (
isNullOrUndefined(source) ||
typeof source !== 'object' ||
!isObject(source) ||
!source.hasOwnProperty(path[index])
) {
return false
Expand Down
5 changes: 3 additions & 2 deletions src/lib/set.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Optional, U } from '../types'
import { shallowCopy } from '../utils'
import { isObject } from 'util'

/**
* Sets the value on the specified path in source value. If the path in the source doesn't exist it
Expand Down Expand Up @@ -122,13 +123,13 @@ export function set<

// NOTE: implementation
export function set(source: any, path: any[], value: any) {
const returnObject = shallowCopy(source, Array.isArray(path[0]) ? [] : {})
const returnObject = shallowCopy(source, Number.isInteger(path[0]) ? [] : {})
let currentObject = returnObject
let index = 0
while (index < path.length) {
if (
!Array.isArray(currentObject[path[index]]) &&
typeof currentObject[path[index]] !== 'object'
!isObject(currentObject[path[index]])
) {
currentObject[path[index]] = Number.isInteger(path[index + 1]) ? [] : {}
}
Expand Down
82 changes: 41 additions & 41 deletions src/lib/unset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ type Unset5<T, K1, K2, K3, K4, K5> = {
* @returns source value with removed value
*/
export function unset<T, K1 extends keyof T>(
source: Nullable<T>,
source: T,
path: [K1],
): Nullable<Unset1<T, K1>>
): Unset1<T, K1>

/**
* Removes the value on the specified path in source value. If the value is an array, the behavior
Expand All @@ -51,9 +51,9 @@ export function unset<T, K1 extends keyof T>(
* @returns source value with removed value
*/
export function unset<T, K1 extends keyof T, K2 extends keyof U<T[K1]>>(
source: Nullable<T>,
source: T,
path: [K1, K2],
): Nullable<Unset2<T, K1, K2>>
): Unset2<T, K1, K2>

/**
* Removes the value on the specified path in source value. If the value is an array, the behavior
Expand All @@ -74,7 +74,7 @@ export function unset<
K1 extends keyof T,
K2 extends keyof U<T[K1]>,
K3 extends keyof U<U<T[K1]>[K2]>
>(source: Nullable<T>, path: [K1, K2, K3]): Nullable<Unset3<T, K1, K2, K3>>
>(source: T, path: [K1, K2, K3]): Unset3<T, K1, K2, K3>

/**
* Removes the value on the specified path in source value. If the value is an array, the behavior
Expand All @@ -96,10 +96,7 @@ export function unset<
K2 extends keyof U<T[K1]>,
K3 extends keyof U<U<T[K1]>[K2]>,
K4 extends keyof U<U<U<T[K1]>[K2]>[K3]>
>(
source: Nullable<T>,
path: [K1, K2, K3, K4],
): Nullable<Unset4<T, K1, K2, K3, K4>>
>(source: T, path: [K1, K2, K3, K4]): Unset4<T, K1, K2, K3, K4>

/**
* Removes the value on the specified path in source value. If the value is an array, the behavior
Expand All @@ -122,10 +119,7 @@ export function unset<
K3 extends keyof U<U<T[K1]>[K2]>,
K4 extends keyof U<U<U<T[K1]>[K2]>[K3]>,
K5 extends keyof U<U<U<U<T[K1]>[K2]>[K3]>[K4]>
>(
source: Nullable<T>,
path: [K1, K2, K3, K4, K5],
): Nullable<Unset5<T, K1, K2, K3, K4, K5>>
>(source: T, path: [K1, K2, K3, K4, K5]): Unset5<T, K1, K2, K3, K4, K5>

/**
* Removes the value on the specified path in source value. If the value is an array, the behavior
Expand All @@ -142,9 +136,9 @@ export function unset<
* @returns source value with removed value
*/
export function unset<T, K1 extends keyof T>(
source: Undefinable<T>,
source: Nullable<T>,
path: [K1],
): Undefinable<Unset1<T, K1>>
): Nullable<Unset1<T, K1>>

/**
* Removes the value on the specified path in source value. If the value is an array, the behavior
Expand All @@ -161,9 +155,9 @@ export function unset<T, K1 extends keyof T>(
* @returns source value with removed value
*/
export function unset<T, K1 extends keyof T, K2 extends keyof U<T[K1]>>(
source: Undefinable<T>,
source: Nullable<T>,
path: [K1, K2],
): Undefinable<Unset2<T, K1, K2>>
): Nullable<Unset2<T, K1, K2>>

/**
* Removes the value on the specified path in source value. If the value is an array, the behavior
Expand All @@ -184,10 +178,7 @@ export function unset<
K1 extends keyof T,
K2 extends keyof U<T[K1]>,
K3 extends keyof U<U<T[K1]>[K2]>
>(
source: Undefinable<T>,
path: [K1, K2, K3],
): Undefinable<Unset3<T, K1, K2, K3>>
>(source: Nullable<T>, path: [K1, K2, K3]): Nullable<Unset3<T, K1, K2, K3>>

/**
* Removes the value on the specified path in source value. If the value is an array, the behavior
Expand All @@ -210,9 +201,9 @@ export function unset<
K3 extends keyof U<U<T[K1]>[K2]>,
K4 extends keyof U<U<U<T[K1]>[K2]>[K3]>
>(
source: Undefinable<T>,
source: Nullable<T>,
path: [K1, K2, K3, K4],
): Undefinable<Unset4<T, K1, K2, K3, K4>>
): Nullable<Unset4<T, K1, K2, K3, K4>>

/**
* Removes the value on the specified path in source value. If the value is an array, the behavior
Expand All @@ -236,9 +227,9 @@ export function unset<
K4 extends keyof U<U<U<T[K1]>[K2]>[K3]>,
K5 extends keyof U<U<U<U<T[K1]>[K2]>[K3]>[K4]>
>(
source: Undefinable<T>,
source: Nullable<T>,
path: [K1, K2, K3, K4, K5],
): Undefinable<Unset5<T, K1, K2, K3, K4, K5>>
): Nullable<Unset5<T, K1, K2, K3, K4, K5>>

/**
* Removes the value on the specified path in source value. If the value is an array, the behavior
Expand All @@ -255,9 +246,9 @@ export function unset<
* @returns source value with removed value
*/
export function unset<T, K1 extends keyof T>(
source: Optional<T>,
source: Undefinable<T>,
path: [K1],
): Optional<Unset1<T, K1>>
): Undefinable<Unset1<T, K1>>

/**
* Removes the value on the specified path in source value. If the value is an array, the behavior
Expand All @@ -274,9 +265,9 @@ export function unset<T, K1 extends keyof T>(
* @returns source value with removed value
*/
export function unset<T, K1 extends keyof T, K2 extends keyof U<T[K1]>>(
source: Optional<T>,
source: Undefinable<T>,
path: [K1, K2],
): Optional<Unset2<T, K1, K2>>
): Undefinable<Unset2<T, K1, K2>>

/**
* Removes the value on the specified path in source value. If the value is an array, the behavior
Expand All @@ -297,7 +288,10 @@ export function unset<
K1 extends keyof T,
K2 extends keyof U<T[K1]>,
K3 extends keyof U<U<T[K1]>[K2]>
>(source: Optional<T>, path: [K1, K2, K3]): Optional<Unset3<T, K1, K2, K3>>
>(
source: Undefinable<T>,
path: [K1, K2, K3],
): Undefinable<Unset3<T, K1, K2, K3>>

/**
* Removes the value on the specified path in source value. If the value is an array, the behavior
Expand All @@ -320,9 +314,9 @@ export function unset<
K3 extends keyof U<U<T[K1]>[K2]>,
K4 extends keyof U<U<U<T[K1]>[K2]>[K3]>
>(
source: Optional<T>,
source: Undefinable<T>,
path: [K1, K2, K3, K4],
): Optional<Unset4<T, K1, K2, K3, K4>>
): Undefinable<Unset4<T, K1, K2, K3, K4>>

/**
* Removes the value on the specified path in source value. If the value is an array, the behavior
Expand All @@ -346,9 +340,9 @@ export function unset<
K4 extends keyof U<U<U<T[K1]>[K2]>[K3]>,
K5 extends keyof U<U<U<U<T[K1]>[K2]>[K3]>[K4]>
>(
source: Optional<T>,
source: Undefinable<T>,
path: [K1, K2, K3, K4, K5],
): Optional<Unset5<T, K1, K2, K3, K4, K5>>
): Undefinable<Unset5<T, K1, K2, K3, K4, K5>>

/**
* Removes the value on the specified path in source value. If the value is an array, the behavior
Expand All @@ -365,9 +359,9 @@ export function unset<
* @returns source value with removed value
*/
export function unset<T, K1 extends keyof T>(
source: T,
source: Optional<T>,
path: [K1],
): Unset1<T, K1>
): Optional<Unset1<T, K1>>

/**
* Removes the value on the specified path in source value. If the value is an array, the behavior
Expand All @@ -384,9 +378,9 @@ export function unset<T, K1 extends keyof T>(
* @returns source value with removed value
*/
export function unset<T, K1 extends keyof T, K2 extends keyof U<T[K1]>>(
source: T,
source: Optional<T>,
path: [K1, K2],
): Unset2<T, K1, K2>
): Optional<Unset2<T, K1, K2>>

/**
* Removes the value on the specified path in source value. If the value is an array, the behavior
Expand All @@ -407,7 +401,7 @@ export function unset<
K1 extends keyof T,
K2 extends keyof U<T[K1]>,
K3 extends keyof U<U<T[K1]>[K2]>
>(source: T, path: [K1, K2, K3]): Unset3<T, K1, K2, K3>
>(source: Optional<T>, path: [K1, K2, K3]): Optional<Unset3<T, K1, K2, K3>>

/**
* Removes the value on the specified path in source value. If the value is an array, the behavior
Expand All @@ -429,7 +423,10 @@ export function unset<
K2 extends keyof U<T[K1]>,
K3 extends keyof U<U<T[K1]>[K2]>,
K4 extends keyof U<U<U<T[K1]>[K2]>[K3]>
>(source: T, path: [K1, K2, K3, K4]): Unset4<T, K1, K2, K3, K4>
>(
source: Optional<T>,
path: [K1, K2, K3, K4],
): Optional<Unset4<T, K1, K2, K3, K4>>

/**
* Removes the value on the specified path in source value. If the value is an array, the behavior
Expand All @@ -452,7 +449,10 @@ export function unset<
K3 extends keyof U<U<T[K1]>[K2]>,
K4 extends keyof U<U<U<T[K1]>[K2]>[K3]>,
K5 extends keyof U<U<U<U<T[K1]>[K2]>[K3]>[K4]>
>(source: T, path: [K1, K2, K3, K4, K5]): Unset5<T, K1, K2, K3, K4, K5>
>(
source: Optional<T>,
path: [K1, K2, K3, K4, K5],
): Optional<Unset5<T, K1, K2, K3, K4, K5>>

// NOTE: implementation
export function unset(source: any, path: any[]) {
Expand Down
5 changes: 3 additions & 2 deletions src/lib/update.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DeepReadonly, Optional, U } from '../types'
import { shallowCopy } from '../utils'
import { isObject } from 'util'

/**
* Updates the value on the specified path in source value using update function. This function will
Expand Down Expand Up @@ -140,13 +141,13 @@ export function update<

// NOTE: implementation
export function update(source: any, path: any[], updateFn: any) {
const returnObject = shallowCopy(source, Array.isArray(path[0]) ? [] : {})
const returnObject = shallowCopy(source, Number.isInteger(path[0]) ? [] : {})
let currentObject = returnObject
let index = 0
while (index < path.length) {
if (
!Array.isArray(currentObject[path[index]]) &&
typeof currentObject[path[index]] !== 'object'
!isObject(currentObject[path[index]])
) {
currentObject[path[index]] = Number.isInteger(path[index + 1]) ? [] : {}
}
Expand Down
21 changes: 14 additions & 7 deletions src/test/set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,21 @@ describe('set', () => {
})
})

test('if path is number an array is created, otherwise object is created', () => {
type A = string[]
type D = { [key: string]: A }
type T = { req: { opt?: D | null } }
const obj: T = { req: { opt: null } }
describe('if path is number an array is created, otherwise object is created', () => {
test('correct root value', () => {
expect(set(null as any, ['hello'], 'str')).toEqual({ hello: 'str' })
expect(set(null as any, [1], 'str')).toEqual([undefined, 'str'])
})

test('deep path', () => {
type A = string[]
type D = { [key: string]: A }
type T = { req: { opt?: D | null } }
const obj: T = { req: { opt: null } }

expect(set(obj, ['req', 'opt', 'key', 1], 'str')).toEqual({
req: { opt: { key: [undefined, 'str'] } },
expect(set(obj, ['req', 'opt', 'key', 1], 'str')).toEqual({
req: { opt: { key: [undefined, 'str'] } },
})
})
})
})
Expand Down
26 changes: 19 additions & 7 deletions src/test/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,26 @@ describe('update', () => {
})
})

test('if path is number an array is created, otherwise object is created', () => {
type A = string[]
type D = { [key: string]: A }
type T = { req: { opt?: D | null } }
const obj: T = { req: { opt: null } }
describe('if path is number an array is created, otherwise object is created', () => {
test('correct root value', () => {
expect(update(null as any, ['hello'], () => 'str')).toEqual({
hello: 'str',
})
expect(update(null as any, [1], () => 'str')).toEqual([
undefined,
'str',
])
})

test('deep path', () => {
type A = string[]
type D = { [key: string]: A }
type T = { req: { opt?: D | null } }
const obj: T = { req: { opt: null } }

expect(update(obj, ['req', 'opt', 'key', 1], () => 'str')).toEqual({
req: { opt: { key: [undefined, 'str'] } },
expect(update(obj, ['req', 'opt', 'key', 1], () => 'str')).toEqual({
req: { opt: { key: [undefined, 'str'] } },
})
})
})
})
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type DeepReadonlyObject<T> = {

export type Nullable<T> = T | null

export type Undefinable<T> = T | null
export type Undefinable<T> = T | undefined

export type Optional<T> = T | null | undefined

Expand Down
Loading

0 comments on commit fa91958

Please sign in to comment.