Skip to content

Commit

Permalink
Run the formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
queicherius committed Jul 4, 2021
1 parent 2a330aa commit cb5920a
Show file tree
Hide file tree
Showing 45 changed files with 64 additions and 205 deletions.
2 changes: 0 additions & 2 deletions src/average/average.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,3 @@
export function average(array: Array<number>): number {
return array.reduce((a, b) => a + b, 0) / array.length
}


6 changes: 1 addition & 5 deletions src/benchmarkHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,7 @@ export class Benchmark {
].join(''),
].join('\n')

fs.writeFileSync(
path.join(__dirname, `./${this.name}/BENCHMARK.md`),
content,
'utf-8'
)
fs.writeFileSync(path.join(__dirname, `./${this.name}/BENCHMARK.md`), content, 'utf-8')
}

generateResultTable(): string {
Expand Down
13 changes: 1 addition & 12 deletions src/chunk/chunk.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,7 @@ describe('chunk', () => {

it('splits an array of objects into chunks', () => {
expect(
chunk(
[
{ a: '1' },
{ a: '2' },
{ a: '3' },
{ a: '4' },
{ a: '5' },
{ a: '6' },
{ a: '7' },
],
4
)
chunk([{ a: '1' }, { a: '2' }, { a: '3' }, { a: '4' }, { a: '5' }, { a: '6' }, { a: '7' }], 4)
).toEqual([
[{ a: '1' }, { a: '2' }, { a: '3' }, { a: '4' }],
[{ a: '5' }, { a: '6' }, { a: '7' }],
Expand Down
2 changes: 0 additions & 2 deletions src/chunk/chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,3 @@ export function chunk<T>(array: Array<T>, size: number): Array<Array<T>> {

return chunked
}


11 changes: 1 addition & 10 deletions src/clone/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@
* ```
*/

type JSONValue =
| string
| number
| boolean
| null
| undefined
| JSONObject
| JSONArray
type JSONValue = string | number | boolean | null | undefined | JSONObject | JSONArray

interface JSONObject {
[key: string]: JSONValue
Expand All @@ -30,5 +23,3 @@ interface JSONArray extends Array<JSONValue> {}
export function clone<T extends JSONValue>(value: T): T {
return JSON.parse(JSON.stringify(value))
}


21 changes: 2 additions & 19 deletions src/compact/compact.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,7 @@ describe('compact', () => {
const original = ['a', false, 'b', null, 'c', 0, 'd', '', 'e', undefined]
const compacted = compact(original)

expect(original).toEqual([
'a',
false,
'b',
null,
'c',
0,
'd',
'',
'e',
undefined,
])
expect(original).toEqual(['a', false, 'b', null, 'c', 0, 'd', '', 'e', undefined])
expect(compacted).toEqual(['a', 'b', 'c', 'd', 'e'])
})

Expand Down Expand Up @@ -55,13 +44,7 @@ describe('compact', () => {
{ e: 1 },
undefined,
])
expect(compacted).toEqual([
{ a: 1 },
{ b: 1 },
{ c: 1 },
{ d: 1 },
{ e: 1 },
])
expect(compacted).toEqual([{ a: 1 }, { b: 1 }, { c: 1 }, { d: 1 }, { e: 1 }])
})

it('compacts completely falsy arrays', () => {
Expand Down
2 changes: 0 additions & 2 deletions src/compact/compact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@ type Falsy = undefined | null | false | 0 | ''
export function compact<T>(array: Array<T | Falsy>): Array<T> {
return array.filter(Boolean) as Array<T>
}


7 changes: 1 addition & 6 deletions src/debounce/debounce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@
* ```
*/

export function debounce<TFunc extends (...args: any[]) => void>(
func: TFunc,
wait: number
) {
export function debounce<TFunc extends (...args: any[]) => void>(func: TFunc, wait: number) {
let timeoutID: number | null = null

return function (this: any, ...args: any[]) {
timeoutID && clearTimeout(timeoutID)
timeoutID = window.setTimeout(() => func.apply(this, args), wait)
} as (...args: Parameters<TFunc>) => void
}


6 changes: 1 addition & 5 deletions src/duplicates/duplicates.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ describe('duplicates', () => {
})

it('filters the duplicate occurrences of an array of strings', () => {
expect(duplicates(['foo', 'bar', 'foo', 'foobar', 'foo', 'foo'])).toEqual([
'foo',
'foo',
'foo',
])
expect(duplicates(['foo', 'bar', 'foo', 'foobar', 'foo', 'foo'])).toEqual(['foo', 'foo', 'foo'])
})

it('filters the duplicate occurrences of an array of objects with an identity function', () => {
Expand Down
12 changes: 2 additions & 10 deletions src/duplicates/duplicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
* ```
*/

export function duplicates<T>(
array: Array<T>,
identity?: (x: T) => any
): Array<T> {
export function duplicates<T>(array: Array<T>, identity?: (x: T) => any): Array<T> {
if (!identity) {
return primitiveDuplicates(array)
}
Expand All @@ -33,12 +30,7 @@ function primitiveDuplicates<T>(array: Array<T>): Array<T> {
return array.filter((x, i, self) => self.indexOf(x) !== i)
}

function objectDuplicates<T>(
array: Array<T>,
identity: (x: T) => any
): Array<T> {
function objectDuplicates<T>(array: Array<T>, identity: (x: T) => any): Array<T> {
const identities = array.map((x) => identity(x))
return array.filter((_, i) => identities.indexOf(identities[i]) !== i)
}


4 changes: 1 addition & 3 deletions src/escapeRegExp/escapeRegExp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { escapeRegExp } from './escapeRegExp'

describe('escapeRegExp', () => {
it('can escape the special characters in a string', () => {
expect(escapeRegExp('How much $$$ for this?')).toEqual(
'How much \\$\\$\\$ for this\\?'
)
expect(escapeRegExp('How much $$$ for this?')).toEqual('How much \\$\\$\\$ for this\\?')
expect(escapeRegExp('\\')).toEqual('\\\\')
expect(escapeRegExp('^^')).toEqual('\\^\\^')
})
Expand Down
2 changes: 0 additions & 2 deletions src/escapeRegExp/escapeRegExp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@
export function escapeRegExp(string: string): string {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}


10 changes: 2 additions & 8 deletions src/generateDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ async function run() {
subText.push(`[Source](./src/${module.name}/index.ts)`)

// Link to the benchmark
const benchmarkPath = path.join(
__dirname,
`./${module.name}/BENCHMARK.md`
)
const benchmarkPath = path.join(__dirname, `./${module.name}/BENCHMARK.md`)
const hasBenchmark = fs.existsSync(benchmarkPath)
if (hasBenchmark) {
subText.push(`[Benchmark](./src/${module.name}/BENCHMARK.md)`)
Expand Down Expand Up @@ -56,10 +53,7 @@ async function run() {
// ----------------------------------------------------------------------------

async function calculateModuleSizes(name: string) {
const content = fs.readFileSync(
path.join(__dirname, `../dist/esm/${name}/${name}.js`),
'utf-8'
)
const content = fs.readFileSync(path.join(__dirname, `../dist/esm/${name}/${name}.js`), 'utf-8')

const contentMin = (await terser.minify(content)).code || ''
const contentMinZip = pako.deflate(contentMin)
Expand Down
10 changes: 5 additions & 5 deletions src/get/BENCHMARK.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

[Source for this benchmark](./benchmark.ts)

| | lodash | flocky |
| --- | --- | --- |
| array path | **577,865 ops/sec (100.00%)** | 460,501 ops/sec (79.69%) |
| string path | 215,076 ops/sec (47.18%) | **455,867 ops/sec (100.00%)** |
| | lodash | flocky |
| ----------- | ----------------------------- | ----------------------------- |
| array path | **577,865 ops/sec (100.00%)** | 460,501 ops/sec (79.69%) |
| string path | 215,076 ops/sec (47.18%) | **455,867 ops/sec (100.00%)** |

<sup>Generated at 2020-06-27 with Node.JS v14.4.0</sup>
<sup>Generated at 2020-06-27 with Node.JS v14.4.0</sup>
2 changes: 0 additions & 2 deletions src/get/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,3 @@ function getWithArrayPath(object: object, path: Array<string | number>): any {

return index === length ? current : undefined
}


8 changes: 4 additions & 4 deletions src/hash/BENCHMARK.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

[Source for this benchmark](./benchmark.ts)

| | flocky |
| --- | --- |
| | flocky |
| ---------- | ----------------------------- |
| 1KB String | **109,262 ops/sec (100.00%)** |
| 1MB String | **122 ops/sec (100.00%)** |
| 1MB String | **122 ops/sec (100.00%)** |

<sup>Generated at 2020-06-27 with Node.JS v14.4.0</sup>
<sup>Generated at 2020-06-27 with Node.JS v14.4.0</sup>
2 changes: 0 additions & 2 deletions src/hash/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,3 @@ function mul32(m: number, n: number) {

return (((nHigh * m) | 0) + ((nLow * m) | 0)) | 0
}


3 changes: 1 addition & 2 deletions src/identifier/identifier.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { dateNow, mathRandom } from '../testHelpers'
import { identifier } from './identifier'

const UUID_FORMAT =
/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
const UUID_FORMAT = /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i

describe('identifier', () => {
beforeEach(() => {
Expand Down
2 changes: 0 additions & 2 deletions src/identifier/identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,3 @@ export function identifier(): string {

return uuid
}


4 changes: 1 addition & 3 deletions src/matchAll/matchAll.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ describe('matchAll', () => {
})

it('matches all occurrences of the regular expression with multiple submatches', () => {
expect(
matchAll(/(\d+) and (\d+)/g, '200 and 400 or also 300 and 500')
).toEqual([
expect(matchAll(/(\d+) and (\d+)/g, '200 and 400 or also 300 and 500')).toEqual([
{ match: '200 and 400', subMatches: ['200', '400'], index: 0 },
{ match: '300 and 500', subMatches: ['300', '500'], index: 20 },
])
Expand Down
2 changes: 0 additions & 2 deletions src/matchAll/matchAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,3 @@ function formatMatch(match: RegExpMatchArray) {
index: match.index,
}
}


2 changes: 0 additions & 2 deletions src/max/max.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,3 @@
export function max(array: Array<number>): number {
return Math.max.apply(null, array)
}


10 changes: 5 additions & 5 deletions src/memoize/BENCHMARK.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

[Source for this benchmark](./benchmark.ts)

| | lodash | fast-memoize | flocky |
| --- | --- | --- | --- |
| monadic | 61,516,142 ops/sec (40.39%) | **152,318,396 ops/sec (100.00%)** | 150,923,324 ops/sec (99.08%) |
| variadic | **2,121,499 ops/sec (100.00%)** | 1,020,555 ops/sec (48.11%) | 1,948,001 ops/sec (91.82%) |
| | lodash | fast-memoize | flocky |
| -------- | ------------------------------- | --------------------------------- | ---------------------------- |
| monadic | 61,516,142 ops/sec (40.39%) | **152,318,396 ops/sec (100.00%)** | 150,923,324 ops/sec (99.08%) |
| variadic | **2,121,499 ops/sec (100.00%)** | 1,020,555 ops/sec (48.11%) | 1,948,001 ops/sec (91.82%) |

<sup>Generated at 2021-02-17 with Node.JS v14.4.0</sup>
<sup>Generated at 2021-02-17 with Node.JS v14.4.0</sup>
4 changes: 1 addition & 3 deletions src/memoize/memoize.benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ benchmark.add({
func: () => flockyMonadicFunc(1),
})

const lodashVariadicFunc = lodash.memoize(variadicFunc, (...args) =>
JSON.stringify(args)
)
const lodashVariadicFunc = lodash.memoize(variadicFunc, (...args) => JSON.stringify(args))
benchmark.add({
library: 'lodash',
input: 'variadic',
Expand Down
10 changes: 3 additions & 7 deletions src/memoize/memoize.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {sleep} from '../sleep/sleep'
import { sleep } from '../sleep/sleep'
import { memoize } from './memoize'

type NumberObject = { n: number }
Expand Down Expand Up @@ -114,10 +114,7 @@ describe('memoize', () => {

it('memoizes function calls with spread non-primitive arguments', () => {
const calls: Array<Array<NumberObject>> = []
const func = (
multiplier: NumberObject,
...numbers: Array<NumberObject>
) => {
const func = (multiplier: NumberObject, ...numbers: Array<NumberObject>) => {
calls.push([multiplier, ...numbers])
return numbers.map((x) => multiplier.n * x.n)
}
Expand All @@ -136,8 +133,7 @@ describe('memoize', () => {
})

it('passes arguments as their original primitive', () => {
const func = (x: any) =>
typeof x === 'object' ? x.constructor.name : typeof x
const func = (x: any) => (typeof x === 'object' ? x.constructor.name : typeof x)

const memoizedFunc = memoize(func)

Expand Down
17 changes: 7 additions & 10 deletions src/memoize/memoize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ type MemoizeStrategy = 'monadic' | 'variadic'

type MemoizeSerializer = (...args: Array<any>) => string

export function memoize<
TThis,
TReturn,
TFunc extends (...args: any[]) => TReturn
>(this: TThis, func: TFunc, options: MemoizeOptions = {}): TFunc {
export function memoize<TThis, TReturn, TFunc extends (...args: any[]) => TReturn>(
this: TThis,
func: TFunc,
options: MemoizeOptions = {}
): TFunc {
const strategy =
options.strategy === 'monadic' ||
(options.strategy !== 'variadic' && func.length <= 1)
options.strategy === 'monadic' || (options.strategy !== 'variadic' && func.length <= 1)
? monadic
: variadic
const cache = options.ttl ? ttlCache(options.ttl) : defaultCache()
Expand All @@ -45,9 +44,7 @@ export function memoize<

function isPrimitive(value: any) {
// We can not treat strings as primitive, because they overwrite numbers
return (
value == null || typeof value === 'number' || typeof value === 'boolean'
)
return value == null || typeof value === 'number' || typeof value === 'boolean'
}

function monadic<TThis, TReturn, TFunc extends (...args: any[]) => TReturn>(
Expand Down
2 changes: 0 additions & 2 deletions src/min/min.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,3 @@
export function min(array: Array<number>): number {
return Math.min.apply(null, array)
}


12 changes: 6 additions & 6 deletions src/omit/BENCHMARK.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

[Source for this benchmark](./benchmark.ts)

| | flocky |
| --- | --- |
| 5 properties / 3 omitted | **5,352,332 ops/sec (100.00%)** |
| 26 properties / 3 omitted | **291,222 ops/sec (100.00%)** |
| 10 properties / 55 omitted | **736,211 ops/sec (100.00%)** |
| | flocky |
| -------------------------- | ------------------------------- |
| 5 properties / 3 omitted | **5,352,332 ops/sec (100.00%)** |
| 26 properties / 3 omitted | **291,222 ops/sec (100.00%)** |
| 10 properties / 55 omitted | **736,211 ops/sec (100.00%)** |

<sup>Generated at 2020-09-14 with Node.JS v14.4.0</sup>
<sup>Generated at 2020-09-14 with Node.JS v14.4.0</sup>
Loading

0 comments on commit cb5920a

Please sign in to comment.