Skip to content

Commit

Permalink
add tests to cover functions (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
sodiray authored Sep 9, 2021
1 parent 129f714 commit 429617c
Show file tree
Hide file tree
Showing 15 changed files with 954 additions and 209 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.coverage/
dist
dist
coverage
8 changes: 8 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@ module.exports = {
"transform": {
"^.+\\.(ts|tsx)$": "ts-jest"
},
"coverageThreshold": {
"global": {
"branches": 100,
"functions": 100,
"lines": 100,
"statements": 100
}
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "radash",
"version": "1.0.8",
"version": "1.1.0",
"description": "A collection of useful and helpful functions",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -11,7 +11,7 @@
"private": false,
"license": "MIT",
"scripts": {
"test": "jest",
"test": "jest --coverage",
"check": "yarn lint && yarn test && yarn build",
"build": "tsc",
"lint": "tslint -p tsconfig.json"
Expand Down
33 changes: 17 additions & 16 deletions src/array.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { iter } from './curry'
import { random } from './number'

/**
* Sorts an array of items into groups. The return value is a map where the keys are
Expand Down Expand Up @@ -36,15 +37,13 @@ export const sum = <T extends number | object>(array: T[], fn?: (item: T) => num
* Get the first item in an array or a default value
*/
export const first = <T>(array: T[], defaultValue: T | null | undefined = null) => {
if (!array) return defaultValue
return array?.length > 0 ? array[0] : defaultValue
}

/**
* Get the last item in an array or a default value
*/
export const last = <T>(array: T[], defaultValue: T | null | undefined = null) => {
if (!array) return defaultValue
return array?.length > 0 ? array[array.length - 1] : defaultValue
}

Expand Down Expand Up @@ -79,11 +78,11 @@ export const replace = <T>(array: T[], index: number, item: T) => {
* Convert an array to a dictionary by mapping each item
* into a dictionary key & value
*/
export const dict = <T>(array: T[], getId: (item: T) => string | number) => {
export const dict = <T, K extends string | number | symbol>(array: T[], getId: (item: T) => K): Record<K, T> => {
return array.reduce((acc, item) => ({
...acc,
[getId(item)]: item
}), {})
}), {} as Record<K, T>)
}

/**
Expand All @@ -104,17 +103,19 @@ export const select = <T, K>(array: T[], mapper: (item: T) => K, condition: (ite
*
* Ex. max([{ num: 1 }, { num: 2 }], x => x.num) == 2
*/
export const max = <T>(array: T[], getter: (item: T) => number) => {
return boil(array, (a, b) => getter(a) > getter(b) ? a : b)
export const max = <T extends number | object>(array: T[], getter?: (item: T) => number) => {
const get = getter ? getter : (v: any) => v
return boil(array, (a, b) => get(a) > get(b) ? a : b)
}

/**
* Min gets the smallest value from a list
*
* Ex. max([{ num: 1 }, { num: 2 }], x => x.num) == 1
*/
export const min = <T>(array: T[], getter: (item: T) => number) => {
return boil(array, (a, b) => getter(a) < getter(b) ? a : b)
export const min = <T extends number | object>(array: T[], getter?: (item: T) => number) => {
const get = getter ? getter : (v: any) => v
return boil(array, (a, b) => get(a) < get(b) ? a : b)
}


Expand All @@ -125,13 +126,13 @@ export const cluster = <T>(list: T[], size: number = 2): T[][] => {
})
}

export const unique = <T, K extends string | number | symbol>(array: T[], getter: (item: T) => K) => {
const valueMap = array.reduce((acc: Record<string | number | symbol, any>, item: T) => {
const value = getter(item)
if (acc[value]) return acc
return { ...acc, [value]: true }
}, {})
return Object.keys(valueMap)
export const unique = <T, K extends string | number | symbol>(array: T[], toKey?: (item: T) => K): T[] => {
const valueMap = array.reduce((acc, item) => {
const key = toKey ? toKey(item) : (item as any as string | number | symbol)
if (acc[key]) return acc
return { ...acc, [key]: item }
}, {} as Record<string | number | symbol, T>)
return Object.values(valueMap)
}

export const shuffle = <T>(array: T[]): T[] => {
Expand All @@ -151,7 +152,7 @@ export const draw = <T>(array: T[]): T | null => {
if (max === 0) {
return null
}
const index = Math.floor(Math.random() * (max - min + 1) + min)
const index = random(0, max - 1)
return array[index]
}

Expand Down
8 changes: 4 additions & 4 deletions src/curry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export const tryit = <ResultType, ErrorType = Error>(func: Func) => async (
}
}

export const proxied = <T, K>(cb: (arg: T) => K): Record<string, (arg: T) => K> => {
export const proxied = <T, K>(handler: (propertyName: T) => K): Record<string, K> => {
return new Proxy({}, {
get: (target, arg: any) => cb(arg)
get: (target, propertyName: any) => handler(propertyName)
})
}

Expand All @@ -44,7 +44,7 @@ const memoize = <T>(
ttl: number
) => {
return function callWithMemo(...args: any): T {
const key = keyFunc ? keyFunc(args) : JSON.stringify({ args })
const key = keyFunc ? keyFunc(...args) : JSON.stringify({ args })
const existing = cache[key]
if (existing !== undefined) {
if (existing.exp > new Date().getTime()) {
Expand All @@ -64,7 +64,7 @@ export const memo = <TFunc extends Function>(func: TFunc, {
key = null,
ttl = 300
}: {
key?: Func<string> | null
key?: Func<any, string> | null
ttl?: number
} = {}) => {
return memoize({}, func as any, key, ttl) as any as TFunc
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export default {
proxied,
partob,
tryit,
try: tryit,
shake,
mapKeys,
mapValues,
Expand Down
8 changes: 4 additions & 4 deletions src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
* Removes (shakes out) any null or undefined entries from
* the given object
*/
export const shake = <T>(obj: Record<string, T>) => {
export const shake = <T extends object = object>(obj: object): T => {
return Object.keys(obj).reduce((acc, key) => {
if (obj[key] === null || obj[key] === undefined) {
return acc
}
else return { ...acc, [key]: obj[key] }
}, {} as Partial<T>)
}, {} as T)
}

/**
Expand All @@ -36,9 +36,9 @@ export const mapValues = <T, K>(obj: Record<string, T>, mapFunc: (item: T) => K)
export const lowerize = <T>(obj: Record<string, T>) => mapKeys(obj, k => k.toLowerCase())
export const upperize = <T>(obj: Record<string, T>) => mapKeys(obj, k => k.toUpperCase())

export const clone = <T>(obj: Record<string, T>) => {
export const clone = <T extends object = object>(obj: T): T => {
return Object.getOwnPropertyNames(obj).reduce((acc, name) => ({
...acc,
[name]: obj[name]
}), {})
}), {} as T)
}
12 changes: 6 additions & 6 deletions src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { iter } from './curry'
*/
export const camal = (...parts: string[]) => {
if (parts.length === 0) return ''
if (parts.length === 1) return parts[1]
return parts.slice(1).reduce((acc, part) => {
if (parts.length === 1) return parts[0]
return parts.reduce((acc, part) => {
return `${acc}${part.charAt(0).toUpperCase()}${part.slice(1)}`
})
}
Expand All @@ -23,8 +23,8 @@ export const camal = (...parts: string[]) => {
*/
export const snake = (...parts: string[]) => {
if (parts.length === 0) return ''
if (parts.length === 1) return parts[1]
return parts.slice(1).reduce((acc, part) => {
if (parts.length === 1) return parts[0]
return parts.reduce((acc, part) => {
return `${acc}_${part.toLowerCase()}`
})
}
Expand All @@ -43,8 +43,8 @@ export const template = (str: string, data: Record<string, any>, regex = /\{\{(.
}

export const uid = (length: number, specials: string = '') => {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' + (specials ?? '')
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' + specials
return iter(length, (acc) => {
return acc + characters.charAt(random(0, characters.length))
return acc + characters.charAt(random(0, characters.length - 1))
}, '')
}
Loading

0 comments on commit 429617c

Please sign in to comment.