Skip to content

Commit 69eb2e8

Browse files
committed
add more docs
1 parent 981bbce commit 69eb2e8

File tree

2 files changed

+88
-21
lines changed

2 files changed

+88
-21
lines changed

readme.md

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,78 @@ toggleItem([1, 2, 3], 3)
104104
```typescript
105105
import { updateByKey } from "array-utils-ts"
106106

107-
const arr = [{ id: 1 }, { id: 2 }]
107+
// prettier-ignore
108+
const arr1 = [{ id: 1, v: 1 }, { id: 2, v: 1 }]
108109

109-
updateByKey([1, 2, 3], 4)
110-
// -> [1, 2, 3, 4]
110+
const arr2 = updateByKey(arr1, "id", { id: 1, v: 2 })
111+
// -> [{ id: 1, v: 2 }, { id: 2, v: 1 }]
112+
// arr1 !== arr2
111113

112-
updateByKey([1, 2, 3], 3)
113-
// -> [1, 2]
114+
const arr3 = updateByKey(arr2, "id", { id: 3, v: 1 })
115+
// -> [{ id: 1, v: 2 }, { id: 2, v: 1 }]
116+
// arr2 === arr3 // note: item not found, nothing changed
117+
```
118+
119+
### upsertByKey
120+
121+
```typescript
122+
import { upsertByKey } from "array-utils-ts"
123+
124+
// prettier-ignore
125+
const arr1 = [{ id: 1, v: 1 }, { id: 2, v: 1 }]
126+
127+
const arr2 = upsertByKey(arr1, "id", { id: 1, v: 2 })
128+
// -> [{ id: 1, v: 2 }, { id: 2, v: 1 }]
129+
// arr1 !== arr2
130+
131+
const arr3 = upsertByKey(arr2, "id", { id: 3, v: 1 })
132+
// -> [{ id: 1, v: 2 }, { id: 2, v: 1 }, { id: 3, v: 1 }]
133+
// arr2 !== arr3
134+
```
135+
136+
### toggleByKey
137+
138+
```typescript
139+
import { toggleByKey } from "array-utils-ts"
140+
141+
// prettier-ignore
142+
const arr1 = [{ id: 1, v: 1 }, { id: 2, v: 1 }]
143+
144+
const arr2 = toggleByKey(arr1, "id", { id: 1, v: 2 })
145+
// -> [{ id: 2, v: 1 }]
146+
// arr1 !== arr2
147+
148+
const arr3 = toggleByKey(arr2, "id", { id: 3, v: 1 })
149+
// -> [{ id: 2, v: 1 }, { id: 3, v: 1 }]
150+
// arr2 !== arr3
151+
```
152+
153+
### isFirstByKey
154+
155+
```typescript
156+
import { isFirstByKey } from "array-utils-ts"
157+
158+
// prettier-ignore
159+
const arr = [{ id: 1, v: 1 }, { id: 2, v: 1 }]
160+
161+
isFirstByKey(arr, "id", { id: 1, v: 2 })
162+
// -> true
163+
164+
isFirstByKey(arr, "id", { id: 2, v: 1 })
165+
// -> false
166+
```
167+
168+
### isLastByKey
169+
170+
```typescript
171+
import { isLastByKey } from "array-utils-ts"
172+
173+
// prettier-ignore
174+
const arr = [{ id: 1, v: 1 }, { id: 2, v: 1 }]
175+
176+
isLastByKey(arr, "id", { id: 1, v: 1 })
177+
// -> false
178+
179+
isLastByKey(arr, "id", { id: 2, v: 1 })
180+
// -> true
114181
```

src/main.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ type NotNullable<T> = Exclude<T, null | undefined>
22
type NotEmpty<T> = Exclude<T, null | undefined | "">
33
type Dict = Record<string, any>
44

5-
export const uniq = <T>(arr: T[]): T[] => {
6-
return Array.from(new Set(arr))
7-
}
5+
// export const uniq = <T>(arr: T[]): T[] => {
6+
// return Array.from(new Set(arr))
7+
// }
88

99
export const filterNullable = <T>(items: T[]): NotNullable<T>[] => {
1010
return items.filter((x): x is NotNullable<T> => x !== undefined && x !== null)
@@ -28,27 +28,27 @@ export const toggleItem = <T>(arr: T[], item: T): T[] => {
2828

2929
// By Key
3030

31-
export const updateByKey = <T extends Dict>(arr: T[], key: keyof T, val: T): T[] => {
32-
if (!arr.map((x) => x[key]).includes(val[key])) return arr
33-
return arr.map((x) => (x[key] === val[key] ? { ...x, ...val } : x))
31+
export const updateByKey = <T extends Dict>(arr: T[], key: keyof T, obj: T): T[] => {
32+
if (!arr.map((x) => x[key]).includes(obj[key])) return arr
33+
return arr.map((x) => (x[key] === obj[key] ? { ...x, ...obj } : x))
3434
}
3535

36-
export const upsertByKey = <T extends Dict>(arr: T[], key: keyof T, val: T): T[] => {
37-
if (arr.map((x) => x[key]).includes(val[key])) {
38-
return arr.map((x) => (x[key] === val[key] ? { ...x, ...val } : x))
36+
export const upsertByKey = <T extends Dict>(arr: T[], key: keyof T, obj: T): T[] => {
37+
if (arr.map((x) => x[key]).includes(obj[key])) {
38+
return arr.map((x) => (x[key] === obj[key] ? { ...x, ...obj } : x))
3939
}
40-
return [...arr, val]
40+
return [...arr, obj]
4141
}
4242

43-
export const toggleByKey = <T extends Dict>(arr: T[], key: keyof T, val: T) => {
43+
export const toggleByKey = <T extends Dict>(arr: T[], key: keyof T, obj: T) => {
4444
const ids = arr.map((x) => x[key])
45-
return ids.includes(val[key]) ? arr.filter((x) => x[key] !== val[key]) : [...arr, val]
45+
return ids.includes(obj[key]) ? arr.filter((x) => x[key] !== obj[key]) : [...arr, obj]
4646
}
4747

48-
export const isFirstByKey = <T extends Dict>(arr: T[], key: keyof T, val: T): boolean => {
49-
return arr.findIndex((x) => x[key] === val[key]) === 0
48+
export const isFirstByKey = <T extends Dict>(arr: T[], key: keyof T, obj: T): boolean => {
49+
return arr.findIndex((x) => x[key] === obj[key]) === 0
5050
}
5151

52-
export const isLastByKey = <T extends Dict>(arr: T[], key: keyof T, val: T): boolean => {
53-
return arr.findIndex((x) => x[key] === val[key]) === arr.length - 1
52+
export const isLastByKey = <T extends Dict>(arr: T[], key: keyof T, obj: T): boolean => {
53+
return arr.findIndex((x) => x[key] === obj[key]) === arr.length - 1
5454
}

0 commit comments

Comments
 (0)