Skip to content

Commit

Permalink
refactor: fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
gquittet committed Nov 3, 2023
1 parent 0a3e866 commit 3404b26
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 49 deletions.
6 changes: 3 additions & 3 deletions src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export const sort = <T>(
if (!array) return []
const asc = (a: T, b: T) => getter(a) - getter(b)
const dsc = (a: T, b: T) => getter(b) - getter(a)
return array.slice().sort(desc === true ? dsc : asc)
return array.slice().sort(desc ? dsc : asc)
}

/**
Expand Down Expand Up @@ -276,7 +276,7 @@ export function min<T>(
* given a list of 10 items and a size of 2, it will return 5
* lists with 2 items each
*/
export const cluster = <T>(list: readonly T[], size: number = 2): T[][] => {
export const cluster = <T>(list: readonly T[], size = 2): T[][] => {
const clusterCount = Math.ceil(list.length / size)
return new Array(clusterCount).fill(null).map((_c: null, i: number) => {
return list.slice(i * size, i * size + size)
Expand Down Expand Up @@ -320,7 +320,7 @@ export function* range<T = number>(
startOrLength: number,
end?: number,
valueOrMapper: T | ((i: number) => T) = i => i as T,
step: number = 1
step = 1
): Generator<T> {
const mapper = isFunction(valueOrMapper) ? valueOrMapper : () => valueOrMapper
const start = end ? startOrLength : 0
Expand Down
2 changes: 1 addition & 1 deletion src/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const map = async <T, K>(
asyncMapFunc: (item: T, index: number) => Promise<K>
): Promise<K[]> => {
if (!array) return []
let result = []
const result = []
let index = 0
for (const value of array) {
const newValue = await asyncMapFunc(value, index++)
Expand Down
6 changes: 3 additions & 3 deletions src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export const get = <TDefault = unknown>(
path: string,
defaultValue?: TDefault
): TDefault => {
const segments = path.split(/[\.\[\]]/g)
const segments = path.split(/[.[\]]/g)
let current: any = value
for (const key of segments) {
if (current === null) return defaultValue as TDefault
Expand Down Expand Up @@ -244,11 +244,11 @@ export const set = <T extends object, K>(
): T => {
if (!initial) return {} as T
if (!path || value === undefined) return initial
const segments = path.split(/[\.\[\]]/g).filter(x => !!x.trim())
const segments = path.split(/[.[\]]/g).filter(x => !!x.trim())
const _set = (node: any) => {
if (segments.length > 1) {
const key = segments.shift() as string
const nextIsNum = toInt(segments[0], null) === null ? false : true
const nextIsNum = toInt(segments[0], null) !== null
node[key] = node[key] === undefined ? (nextIsNum ? [] : {}) : node[key]
_set(node[key])
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const shuffle = <T>(array: readonly T[]): T[] => {
.map(a => a.value)
}

export const uid = (length: number, specials: string = '') => {
export const uid = (length: number, specials = '') => {
const characters =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' + specials
return iterate(
Expand Down
21 changes: 9 additions & 12 deletions src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const camel = (str: string): string => {
const parts =
str
?.replace(/([A-Z])+/g, capitalize)
?.split(/(?=[A-Z])|[\.\-\s_]/)
?.split(/(?=[A-Z])|[.\-\s_]/)
.map(x => x.toLowerCase()) ?? []
if (parts.length === 0) return ''
if (parts.length === 1) return parts[0]
Expand All @@ -46,7 +46,7 @@ export const snake = (
const parts =
str
?.replace(/([A-Z])+/g, capitalize)
.split(/(?=[A-Z])|[\.\-\s_]/)
.split(/(?=[A-Z])|[.\-\s_]/)
.map(x => x.toLowerCase()) ?? []
if (parts.length === 0) return ''
if (parts.length === 1) return parts[0]
Expand All @@ -55,7 +55,7 @@ export const snake = (
})
return options?.splitOnNumber === false
? result
: result.replace(/([A-Za-z]{1}[0-9]{1})/, val => `${val[0]!}_${val[1]!}`)
: result.replace(/([A-Za-z][0-9])/, val => `${val[0]!}_${val[1]!}`)
}

/**
Expand All @@ -69,7 +69,7 @@ export const dash = (str: string): string => {
const parts =
str
?.replace(/([A-Z])+/g, capitalize)
?.split(/(?=[A-Z])|[\.\-\s_]/)
?.split(/(?=[A-Z])|[.\-\s_]/)
.map(x => x.toLowerCase()) ?? []
if (parts.length === 0) return ''
if (parts.length === 1) return parts[0]
Expand All @@ -85,7 +85,7 @@ export const dash = (str: string): string => {
* pascal('va va boom') -> 'VaVaBoom'
*/
export const pascal = (str: string): string => {
const parts = str?.split(/[\.\-\s_]/).map(x => x.toLowerCase()) ?? []
const parts = str?.split(/[.\-\s_]/).map(x => x.toLowerCase()) ?? []
if (parts.length === 0) return ''
return parts.map(str => str.charAt(0).toUpperCase() + str.slice(1)).join('')
}
Expand All @@ -101,7 +101,7 @@ export const pascal = (str: string): string => {
export const title = (str: string | null | undefined): string => {
if (!str) return ''
return str
.split(/(?=[A-Z])|[\.\-\s_]/)
.split(/(?=[A-Z])|[.\-\s_]/)
.map(s => s.trim())
.filter(s => !!s)
.map(s => capitalize(s.toLowerCase()))
Expand All @@ -118,7 +118,7 @@ export const title = (str: string | null | undefined): string => {
export const template = (
str: string,
data: Record<string, any>,
regex = /\{\{(.+?)\}\}/g
regex = /\{\{(.+?)}}/g
) => {
return Array.from(str.matchAll(regex)).reduce((acc, match) => {
return acc.replace(match[0], data[match[1]])
Expand All @@ -138,12 +138,9 @@ export const template = (
* trim('222222__hello__1111111', '12_') // => 'hello'
* ```
*/
export const trim = (
str: string | null | undefined,
charsToTrim: string = ' '
) => {
export const trim = (str: string | null | undefined, charsToTrim = ' ') => {
if (!str) return ''
const toTrim = charsToTrim.replace(/[\W]{1}/g, '\\$&')
const toTrim = charsToTrim.replace(/\W/g, '\\$&')
const regex = new RegExp(`^[${toTrim}]+|[${toTrim}]+$`, 'g')
return str.replace(regex, '')
}
16 changes: 8 additions & 8 deletions src/tests/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ describe('array module', () => {
describe('range function', () => {
const obj = { name: 'radash' }
const toList = <T>(gen: Generator<T>): T[] => {
let items: T[] = []
const items: T[] = []
for (const item of gen) items.push(item)
return items
}
Expand Down Expand Up @@ -520,19 +520,19 @@ describe('array module', () => {

describe('merge function', () => {
test('returns empty array for two null inputs', () => {
const result = _.merge(NULL, NULL, x => '')
const result = _.merge(NULL, NULL, () => '')
assert.deepEqual(result, [])
})
test('returns an empty array for two empty array inputs', () => {
const result = _.merge([], [], x => '')
const result = _.merge([], [], () => '')
assert.deepEqual(result, [])
})
test('returns root for a null other input', () => {
const result = _.merge([], NULL, x => '')
const result = _.merge([], NULL, () => '')
assert.deepEqual(result, [])
})
test('returns empty array for a null root input', () => {
const result = _.merge(NULL, [], x => '')
const result = _.merge(NULL, [], () => '')
assert.deepEqual(result, [])
})
test('returns root for a null matcher input', () => {
Expand Down Expand Up @@ -569,15 +569,15 @@ describe('array module', () => {
const lettersXE = ['a', 'b', 'c', 'd', 'XE']
const lettersXX = ['a', 'b', 'c', 'd', 'e', 'XX']
test('returns empty array for two null inputs', () => {
const result = _.replaceOrAppend(NULL, null, x => false)
const result = _.replaceOrAppend(NULL, null, () => false)
assert.deepEqual(result, [])
})
test('returns array with new item for null list input', () => {
const result = _.replaceOrAppend(NULL, 'a', x => false)
const result = _.replaceOrAppend(NULL, 'a', () => false)
assert.deepEqual(result, ['a'])
})
test('returns list for null new item input', () => {
const result = _.replaceOrAppend(['a'], null, x => false)
const result = _.replaceOrAppend(['a'], null, () => false)
assert.deepEqual(result, ['a'])
})
test('returns list with item replacing match by index', () => {
Expand Down
14 changes: 6 additions & 8 deletions src/tests/async.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ describe('async module', () => {
defer(async () => {
three = 3
})
if (!!true) throw new Error('soooo broken')
return 'x'
throw new Error('soooo broken')
})
} catch {}
assert.equal(one, 1)
Expand Down Expand Up @@ -366,7 +365,7 @@ describe('async module', () => {
const NULL = null as unknown as Options

test('returns result of given function', async () => {
const result = await _.retry(NULL, async bail => {
const result = await _.retry(NULL, async () => {
return 'hello'
})
assert.equal(result, 'hello')
Expand All @@ -379,7 +378,7 @@ describe('async module', () => {
})
test('retries on failure', async () => {
let failedOnce = false
const result = await _.retry(NULL, async bail => {
const result = await _.retry(NULL, async () => {
if (!failedOnce) {
failedOnce = true
throw 'Failing for test'
Expand Down Expand Up @@ -436,7 +435,7 @@ describe('async module', () => {
})
test('uses backoff between retries', async () => {
let count = 0
let backoffs: number = 0
let backoffs = 0
const start = Date.now()
await _.retry(
{
Expand Down Expand Up @@ -486,10 +485,9 @@ describe('async module', () => {
})
it('returns error if given sync function throws', async () => {
const alwaysThrow = () => {
if (1 > 0) throw new Error('error')
return undefined
throw new Error('error')
}
const result = _.guard(alwaysThrow) ?? 'good-bye'
const result = (await _.guard(alwaysThrow)) ?? 'good-bye'
assert.equal(result, 'good-bye')
})
it('throws error if shouldGuard returns false', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/tests/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ describe('object module', () => {
})
test('copies all attributes from class instance', () => {
class Data {
public x: number = 22
public x = 22
public add(a: number, b: number) {
return a + b
}
Expand Down Expand Up @@ -501,7 +501,7 @@ describe('object module', () => {
cards: [[{ value: 2 }]]
})
assert.deepEqual(_.set({}, 'cards.[1].[1].value', 2), {
cards: [, [, { value: 2 }]]
cards: [undefined, [undefined, { value: 2 }]]
})
})
})
Expand Down
18 changes: 9 additions & 9 deletions src/tests/typed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ describe('typed module', () => {
for (const elm of arr) {
assert.isTrue(_.isPrimitive(elm))
}
}),
test('returns false for non-primitives', () => {
const arr = [new Date(), Number, {}, Object({}), () => 0, [1, 2]]
})
test('returns false for non-primitives', () => {
const arr = [new Date(), Number, {}, Object({}), () => 0, [1, 2]]

for (const elm of arr) {
assert.isFalse(_.isPrimitive(elm))
}
})
for (const elm of arr) {
assert.isFalse(_.isPrimitive(elm))
}
})
})

describe('isFunction function', () => {
Expand Down Expand Up @@ -295,7 +295,7 @@ describe('typed module', () => {
describe('isEmpty function', () => {
class Data {}
class Person {
name: string = 'ray'
name = 'ray'
}
test('returns true for empty values', () => {
assert.isTrue(_.isEmpty(null))
Expand Down Expand Up @@ -388,7 +388,7 @@ describe('typed module', () => {
})
test('returns false for class instance with properties', () => {
class Data {
name: string = 'ray'
name = 'ray'
}
const input = new Data()
const result = _.isSymbol(input)
Expand Down
3 changes: 1 addition & 2 deletions src/typed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ export const isDate = (value: any): value is Date => {
export const isPromise = (value: any): value is Promise<any> => {
if (!value) return false
if (!value.then) return false
if (!isFunction(value.then)) return false
return true
return isFunction(value.then)
}

export const isEmpty = (value: any) => {
Expand Down

0 comments on commit 3404b26

Please sign in to comment.