-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b144e01
Showing
39 changed files
with
1,628 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": [ | ||
"plugin:@dewebsitejongens/eslint-plugin/recommended" | ||
], | ||
"parserOptions": { | ||
"project": "./tsconfig.json" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.git | ||
build | ||
types | ||
node_modules | ||
*.lock |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# @dewebsitejongens/utilities |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (c) 2021 De Website Jongens. All rights reserved. | ||
* | ||
* Licensed under the terms of the LICENSE file distributed with this project. | ||
*/ | ||
|
||
module.exports = { | ||
verbose: true, | ||
moduleFileExtensions: [ | ||
'js', | ||
'json', | ||
'ts', | ||
], | ||
rootDir: '.', | ||
testRegex: '.(spec|test).ts$', | ||
transform: { | ||
'^.+\\.(t|j)s$': 'ts-jest', | ||
}, | ||
testPathIgnorePatterns: [ | ||
'/node_modules/', | ||
'/build/', | ||
], | ||
collectCoverageFrom: [ | ||
'**/*.(t|j)s', | ||
], | ||
coverageDirectory: './coverage', | ||
testEnvironment: 'jsdom', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
{ | ||
"name": "@dewebsitejongens/utilities", | ||
"version": "0.1.0", | ||
"description": "De Website Jongens TypeScript Utilities.", | ||
"authors": [ | ||
{ | ||
"name": "Nick Vlug", | ||
"email": "nick@dewebsitejongens.nl" | ||
} | ||
], | ||
"license": "GPL-3.0", | ||
"keywords": [ | ||
"websitejongens", | ||
"dewebsitejongens", | ||
"utilities" | ||
], | ||
"homepage": "https://github.com/dewebsitejongens/utilities/tree/main/README.md", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/dewebsitejongens/utilities.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/dewebsitejongens/utilities/issues" | ||
}, | ||
"scripts": { | ||
"bundle": "tsc", | ||
"bundle:watch": "tsc -w", | ||
"lint": "eslint --fix --ext .tsx,.ts src/**", | ||
"test": "jest", | ||
"test:watch": "jest --watchAll" | ||
}, | ||
"devDependencies": { | ||
"@dewebsitejongens/eslint-plugin": "git+https://github.com/dewebsitejongens/eslint-plugin.git#v0.1.0", | ||
"@types/jest": "^27.0.0", | ||
"eslint": "^7.32.0", | ||
"jest": "^27.0.6", | ||
"ts-jest": "^27.0.4", | ||
"typescript": "^4.3.5" | ||
}, | ||
"engines": { | ||
"node": ">=14", | ||
"yarn": ">=1.22" | ||
}, | ||
"files": [ | ||
"LICENSE", | ||
"README.md", | ||
"build", | ||
"types" | ||
], | ||
"main": "build/index.js", | ||
"types": "types/index.d.ts", | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright (c) 2021 De Website Jongens. All rights reserved. | ||
* | ||
* Licensed under the terms of the LICENSE file distributed with this project. | ||
*/ | ||
|
||
import arrayEach from './array-each'; | ||
|
||
describe('array-each', () => { | ||
it('should iterate an array', () => { | ||
const iterator = jest.fn(); | ||
|
||
arrayEach([1, 2, 3, 4], iterator); | ||
|
||
expect(iterator).toHaveBeenCalledTimes(4); | ||
expect(iterator).toHaveBeenCalledWith(1, 0, 4, [1, 2, 3, 4]); | ||
expect(iterator).toHaveBeenCalledWith(2, 1, 4, [1, 2, 3, 4]); | ||
expect(iterator).toHaveBeenCalledWith(3, 2, 4, [1, 2, 3, 4]); | ||
expect(iterator).toHaveBeenCalledWith(4, 3, 4, [1, 2, 3, 4]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright (c) 2021 De Website Jongens. All rights reserved. | ||
* | ||
* Licensed under the terms of the LICENSE file distributed with this project. | ||
*/ | ||
|
||
type TIterator<T> = (item: T, index: number, length: number, collection: T[]) => void; | ||
|
||
/** | ||
* Array each. | ||
* | ||
* @param collection | ||
* @param iterator | ||
*/ | ||
const arrayEach = <T>(collection: T[], iterator: TIterator<T>) => { | ||
for (let i = 0, { length } = collection; i < length; i += 1) { | ||
iterator(collection[i], i, length, collection); | ||
} | ||
}; | ||
|
||
export default arrayEach; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright (c) 2021 De Website Jongens. All rights reserved. | ||
* | ||
* Licensed under the terms of the LICENSE file distributed with this project. | ||
*/ | ||
|
||
import arrayFind from './array-find'; | ||
|
||
describe('array-find', () => { | ||
it('should iterate an array', () => { | ||
const obj = [ | ||
10, | ||
20, | ||
30, | ||
40, | ||
]; | ||
|
||
expect(arrayFind(obj, (value) => value === 10)).toEqual(obj[0]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright (c) 2021 De Website Jongens. All rights reserved. | ||
* | ||
* Licensed under the terms of the LICENSE file distributed with this project. | ||
*/ | ||
|
||
type TPredicate<T> = (value: T, index: number, length: number, obj: T[]) => boolean; | ||
|
||
/** | ||
* Array find. | ||
* | ||
* @param collection | ||
* @param predicate | ||
*/ | ||
const arrayFind = <T>( | ||
collection: T[], | ||
predicate: TPredicate<T>, | ||
): T | null => collection.find(( | ||
value, | ||
index, | ||
obj, | ||
) => predicate(value, index, obj.length, obj)) || null; | ||
|
||
export default arrayFind; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright (c) 2021 De Website Jongens. All rights reserved. | ||
* | ||
* Licensed under the terms of the LICENSE file distributed with this project. | ||
*/ | ||
|
||
import arrayReduce from './array-reduce'; | ||
|
||
describe('arrayReduce', () => { | ||
it('should reduce an array', () => { | ||
expect(arrayReduce([1, 2, 3], (out, value) => out + value, 0)).toEqual(6); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (c) 2021 De Website Jongens. All rights reserved. | ||
* | ||
* Licensed under the terms of the LICENSE file distributed with this project. | ||
*/ | ||
|
||
import arrayEach from './array-each'; | ||
|
||
type TReducer<T> = ( | ||
initialValue: any, | ||
item: T, | ||
index: number, | ||
length: number, | ||
collection: T[] | ||
) => T; | ||
|
||
/** | ||
* Array reduce. | ||
* | ||
* @param collection | ||
* @param reducer | ||
* @param initialValue | ||
*/ | ||
const arrayReduce = <T>(collection: T[], reducer: TReducer<T>, initialValue: any) => { | ||
let returnValue = initialValue; | ||
|
||
arrayEach<T>(collection, (item, index, length) => { | ||
returnValue = reducer(returnValue, item, index, length, collection); | ||
}); | ||
|
||
return returnValue; | ||
}; | ||
|
||
export default arrayReduce; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright (c) 2021 De Website Jongens. All rights reserved. | ||
* | ||
* Licensed under the terms of the LICENSE file distributed with this project. | ||
*/ | ||
|
||
/** | ||
* Debounce function. | ||
* | ||
* @param func | ||
* @param ms | ||
*/ | ||
const debounce = (func: Function, ms = 300) => { | ||
let timeoutId: ReturnType<typeof setTimeout>; | ||
|
||
return function base(this: any, ...args: any[]) { | ||
clearTimeout(timeoutId); | ||
|
||
timeoutId = setTimeout(() => func.apply(this, args), ms); | ||
}; | ||
}; | ||
|
||
export default debounce; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (c) 2021 De Website Jongens. All rights reserved. | ||
* | ||
* Licensed under the terms of the LICENSE file distributed with this project. | ||
*/ | ||
|
||
import get from './get'; | ||
|
||
describe('get', () => { | ||
it('should get value from object', () => { | ||
const obj = { | ||
test: { | ||
test: 1, | ||
}, | ||
arr: [ | ||
{ | ||
test: 1, | ||
}, | ||
15, | ||
], | ||
}; | ||
|
||
expect(get(obj, 'test.test')).toBe(1); | ||
expect(get(obj, 'arr.1')).toBe(15); | ||
expect(get(obj, 'arr.0.test')).toBe(1); | ||
|
||
expect(get(obj, ['test', 'test'])).toBe(1); | ||
expect(get(obj, ['arr', 1])).toBe(15); | ||
expect(get(obj, ['arr', 0, 'test'])).toBe(1); | ||
}); | ||
|
||
it('should get value from array', () => { | ||
const arr = [ | ||
{ | ||
test: 1, | ||
}, | ||
15, | ||
]; | ||
|
||
expect(get(arr, '0.test')).toBe(1); | ||
expect(get(arr, 1)).toBe(15); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright (c) 2021 De Website Jongens. All rights reserved. | ||
* | ||
* Licensed under the terms of the LICENSE file distributed with this project. | ||
*/ | ||
|
||
import isArray from './is-array'; | ||
import isString from './is-string'; | ||
|
||
/** | ||
* Dot-notation get. | ||
* | ||
* @param obj | ||
* @param key | ||
* @param def | ||
*/ | ||
const get = (obj, key, def?) => { | ||
let k: any[] = []; | ||
|
||
if (isString(key)) { | ||
k = key.split('.'); | ||
} else if (!isArray(key)) { | ||
k = [key]; | ||
} else if (isArray(key)) { | ||
k = key; | ||
} | ||
|
||
if (k.length === 0) { | ||
return def; | ||
} | ||
|
||
let newObj = obj; | ||
for (let i = 0; i < k.length; i += 1) { | ||
newObj = newObj ? newObj[k[i]] : undefined; | ||
} | ||
|
||
return typeof newObj === 'undefined' ? def : newObj; | ||
}; | ||
|
||
export default get; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (c) 2021 De Website Jongens. All rights reserved. | ||
* | ||
* Licensed under the terms of the LICENSE file distributed with this project. | ||
*/ | ||
|
||
import arrayEach from './array-each'; | ||
import arrayFind from './array-find'; | ||
import arrayReduce from './array-reduce'; | ||
import debounce from './debounce'; | ||
import get from './get'; | ||
import invariant from './invariant'; | ||
import isArray from './is-array'; | ||
import isBrowser from './is-browser'; | ||
import isNumber from './is-number'; | ||
import isObject from './is-object'; | ||
import isString from './is-string'; | ||
import objectEach from './object-each'; | ||
import objectFind from './object-find'; | ||
import objectMerge from './object-merge'; | ||
import objectReduce from './object-reduce'; | ||
import omit from './omit'; | ||
import throttle from './throttle'; | ||
import warning from './warning'; | ||
|
||
export { | ||
arrayEach, | ||
arrayFind, | ||
arrayReduce, | ||
debounce, | ||
get, | ||
invariant, | ||
isArray, | ||
isBrowser, | ||
isNumber, | ||
isObject, | ||
isString, | ||
objectEach, | ||
objectFind, | ||
objectMerge, | ||
objectReduce, | ||
omit, | ||
throttle, | ||
warning, | ||
}; |
Oops, something went wrong.