Skip to content

Commit

Permalink
chore: move shared types around
Browse files Browse the repository at this point in the history
  • Loading branch information
zacanger committed Jan 8, 2024
1 parent 4a5f230 commit 3f2f855
Show file tree
Hide file tree
Showing 21 changed files with 60 additions and 124 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
* Remove anything that can be done with plain JS now
* Aim for no useless imports (avoid the lodash problem)
* Minimize extraneous imports (avoid the lodash problem)
* Get high test coverage:
* Unit tests (https://github.com/nodejs/help/issues/3902)
* Minimal `any` and `ts-expect-error`
Expand Down
98 changes: 7 additions & 91 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"node": ">=21.0.0"
},
"devDependencies": {
"@types/node": "20.10.6",
"@types/node": "20.10.7",
"eslint": "8.56.0",
"eslint-config-standard-with-typescript": "43.0.0",
"npm-run-all": "4.1.5",
Expand Down
4 changes: 3 additions & 1 deletion src/collect-by.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { type AnyMap } from './types'

/**
* Collect an an array of objects by string key
* cred: gh:uniqname
Expand All @@ -7,7 +9,7 @@
*/

export const collectBy = (p: string) =>
(a: any[]): Record<string, any> =>
(a: any[]): AnyMap =>
a.reduce((c, i) => ({
...c, [i[p]]: i,
}), {})
6 changes: 4 additions & 2 deletions src/count-items-in-array.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
type R = Record<string, number>

/**
* Get an object of items in an array with count
* @example
* countItemsInArray([ 1, 1, 2, 3, 4 ]) // => { '1': 2, '2': 1, '3': 1, '4': 1 }
*/

export const countItemsInArray = <T>(arr: T[]): Record<string, number> =>
arr.reduce((a: Record<string, number>, c: any): Record<string, number> => {
export const countItemsInArray = <T>(arr: T[]): R =>
arr.reduce((a: R, c: any): R => {
if (a[c.toString()]) {
a[c.toString()]++
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/each.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isArrayLike } from './is-array-like'
import { type AnyMap } from './types'

const iterateArray = <T>(
arr: T[],
Expand All @@ -7,7 +8,6 @@ const iterateArray = <T>(
return arr.map((x: T, i: number) => fn(x, i.toString()))
}

type AnyMap = Record<string, any>
type IOFn <T> = (a: T, b: string) => AnyMap
const iterateObject = <T>(
obj: AnyMap,
Expand Down
5 changes: 3 additions & 2 deletions src/filter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { each } from './each'
import { type AnyMap } from './types'

type AnyMapOrArr = Record<string, any> | any[]
type AnyMapOrArr = AnyMap | any[]

/**
* `filter` for array and object
Expand All @@ -12,7 +13,7 @@ type AnyMapOrArr = Record<string, any> | any[]

export const filter = <T>(
fn: (x: T, y: string | number) => boolean,
list: T[] | string | Record<string, any>,
list: T[] | string | AnyMap,
): Record<string, T> | T[] => {
const isArr = Array.isArray(list)
const filtered: AnyMapOrArr = isArr ? [] : {}
Expand Down
4 changes: 3 additions & 1 deletion src/get-key-by-value.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { type AnyMap } from './types'

/**
* Get the key for a value
* @example
Expand All @@ -6,6 +8,6 @@

export const getKeyByValue = (
value: any,
object: Record<string, any>,
object: AnyMap,
): string | undefined =>
Object.keys(object).find((key: string) => object[key] === value)
4 changes: 3 additions & 1 deletion src/group-by.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { type AnyMap } from './types'

/**
* Collect an an array of objects by string key
* @example
Expand All @@ -6,7 +8,7 @@
*/

export const groupBy = (p: string) =>
(a: any[]): Record<string, any> =>
(a: any[]): AnyMap =>
a.reduce((c, i) => ({
...c, [i[p]]: i,
}), {})
4 changes: 3 additions & 1 deletion src/has.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { type AnyMap } from './types'

/**
* See if an object has a property
* @example
* has('a' { b: 'c' }) // => false
*/

export const has = (p: string, o: Record<string, any>): boolean =>
export const has = (p: string, o: AnyMap): boolean =>
Object.prototype.hasOwnProperty.call(o, p)
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export * from './touch'
export * from './truncate'
export * from './try-json'
export * from './type-of'
export * from './types'
export * from './union'
export * from './uniq'
export * from './uniq-by'
Expand Down
3 changes: 2 additions & 1 deletion src/keep.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { filter } from './filter'
import { type AnyMap } from './types'

type R = string | any[] | Record<string, any>
type R = string | any[] | AnyMap

/**
* Returns an array or object with all falsey values removed
Expand Down
8 changes: 5 additions & 3 deletions src/map-object.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { type AnyMap } from './types'

/**
* Map for objects
* @example
Expand All @@ -8,10 +10,10 @@

export const mapObject = (
f: (a: any, b: string, c: any) => any,
o: Record<string, any>,
o: AnyMap,
ctx: any = this,
): Record<string, any> => {
const res: Record<string, any> = {}
): AnyMap => {
const res: AnyMap = {}
Object.keys(o).forEach((k): void => {
res[k] = f.call(ctx, o[k], k, o)
})
Expand Down
8 changes: 5 additions & 3 deletions src/pick.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { type AnyMap } from './types'

/**
* `pick`
* as it is in rambda (not ramda)
Expand All @@ -8,9 +10,9 @@

export const pick = (
ks: string | string[],
o: Record<string, any>,
): Record<string, any> => {
const r: Record<string, any> = {}
o: AnyMap,
): AnyMap => {
const r: AnyMap = {}
let c: number = 0
while (c < ks.length) {
if (ks[c] in o) {
Expand Down
2 changes: 1 addition & 1 deletion src/read-json.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFile } from 'node:fs/promises'
import { type AnyMap } from './types'

type AnyMap = Record<string, any>
type AnyJson = AnyMap | any[]

/**
Expand Down
3 changes: 2 additions & 1 deletion src/safe-get.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { id } from './id'
import { type AnyMap } from './types'

/**
* Like `_.get`: takes an access string and an optional fallback,
Expand All @@ -10,7 +11,7 @@ import { id } from './id'
*/

export const safeGet = <A>(path: string, fallback?: A) => (
obj: Record<string, any>,
obj: AnyMap,
): A | null | undefined =>
(path
.split(/[.[\]]/)
Expand Down
3 changes: 1 addition & 2 deletions src/sort-object.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { isObject } from './is-object'
import { type AnyMap } from './types'

/**
* Sort an object (recursively)
* @example
* sortObject({ b: 'c', a: 'd' }) // => { a: 'd', b: 'c' }
*/

type AnyMap = Record<string, any>

export const sortObject = (o: AnyMap): AnyMap => {
if (!isObject(o)) throw new Error('Expected an object')
const ks = Object.keys(o)
Expand Down
Loading

0 comments on commit 3f2f855

Please sign in to comment.