Skip to content

Commit

Permalink
feat: source base.
Browse files Browse the repository at this point in the history
  • Loading branch information
HeroBanana committed Aug 12, 2021
0 parents commit b144e01
Show file tree
Hide file tree
Showing 39 changed files with 1,628 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": [
"plugin:@dewebsitejongens/eslint-plugin/recommended"
],
"parserOptions": {
"project": "./tsconfig.json"
}
}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.git
build
types
node_modules
*.lock
641 changes: 641 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @dewebsitejongens/utilities
28 changes: 28 additions & 0 deletions jest.config.js
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',
};
55 changes: 55 additions & 0 deletions package.json
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"
}
}
21 changes: 21 additions & 0 deletions src/array-each.test.ts
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]);
});
});
21 changes: 21 additions & 0 deletions src/array-each.ts
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;
20 changes: 20 additions & 0 deletions src/array-find.test.ts
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]);
});
});
24 changes: 24 additions & 0 deletions src/array-find.ts
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;
13 changes: 13 additions & 0 deletions src/array-reduce.test.ts
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);
});
});
34 changes: 34 additions & 0 deletions src/array-reduce.ts
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;
23 changes: 23 additions & 0 deletions src/debounce.ts
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;
43 changes: 43 additions & 0 deletions src/get.test.ts
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);
});
});
40 changes: 40 additions & 0 deletions src/get.ts
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;
45 changes: 45 additions & 0 deletions src/index.ts
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,
};
Loading

0 comments on commit b144e01

Please sign in to comment.