Skip to content
This repository was archived by the owner on Jul 28, 2024. It is now read-only.

Commit aa3ffe8

Browse files
committed
Create library
0 parents  commit aa3ffe8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+7022
-0
lines changed

Diff for: .coveralls.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
service_name: travis-ci
2+
json_path: temp/coveralls.json

Diff for: .eslintignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.history
2+
/dist
3+
/node_modules

Diff for: .eslintrc.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
extends: require.resolve('@wavevision/coding-standard/ts/eslint/base'),
3+
parserOptions: {
4+
project: 'tsconfig.json',
5+
tsconfigRootDir: '.',
6+
},
7+
};

Diff for: .gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.log
2+
/.history
3+
/.idea
4+
!/.idea/codeStyles/codeStyleConfig.xml
5+
!/.idea/codeStyles/Project.xml
6+
/dist
7+
/node_modules
8+
/temp
9+
/.eslintcache

Diff for: .npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
scripts-prepend-node-path=true

Diff for: .nvmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
12.13.0

Diff for: .prettierignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.history
2+
/dist
3+
/node_modules

Diff for: .travis.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
os: linux
2+
dist: xenial
3+
4+
cache:
5+
yarn: true
6+
directories:
7+
- node_modules
8+
9+
git:
10+
depth: 1
11+
12+
language: node_js
13+
14+
jobs:
15+
include:
16+
- script: yarn ci

Diff for: LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Wavevision s.r.o.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Diff for: jest.config.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module.exports = {
2+
globals: {
3+
'ts-jest': {
4+
isolatedModules: true,
5+
},
6+
},
7+
testMatch: ['<rootDir>/src/**/tests/*.test.ts'],
8+
testPathIgnorePatterns: ['<rootDir>/node_modules'],
9+
moduleDirectories: ['node_modules', 'src'],
10+
moduleFileExtensions: ['js', 'ts'],
11+
coverageDirectory: '<rootDir>/temp/coverage',
12+
coverageReporters: ['html', 'lcov', 'text'],
13+
collectCoverageFrom: [
14+
'src/**/*.ts',
15+
'!src/**/index.ts',
16+
'!src/**/*.test.ts',
17+
'!**/node_modules/**',
18+
],
19+
preset: 'ts-jest',
20+
};

Diff for: package.json

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"name": "@wavevision/ts-utils",
3+
"version": "0.1.0",
4+
"description": "Useful TypeScript utilities and helpers",
5+
"keywords": [
6+
"typescript",
7+
"utilities"
8+
],
9+
"main": "index.js",
10+
"author": "Wavevision s.r.o. <[email protected]>",
11+
"contributors": [
12+
{
13+
"name": "Vít Rozsíval",
14+
"email": "[email protected]"
15+
}
16+
],
17+
"license": "MIT",
18+
"repository": {
19+
"type": "git",
20+
"url": "git+https://github.com/wavevision/ts-utils.git"
21+
},
22+
"bugs": {
23+
"url": "https://github.com/wavevision/ts-utils/issues"
24+
},
25+
"homepage": "https://github.com/wavevision/ts-utils",
26+
"devDependencies": {
27+
"@types/jest": "^24.0.25",
28+
"@types/react": "^16.9.17",
29+
"@typescript-eslint/eslint-plugin": "^2.15.0",
30+
"@typescript-eslint/parser": "^2.15.0",
31+
"@wavevision/coding-standard": "^2.1.6",
32+
"coveralls": "^3.0.9",
33+
"eslint": "^6.8.0",
34+
"jest": "^24.9.0",
35+
"npm-run-all": "^4.1.5",
36+
"react": "^16.12.0",
37+
"ts-jest": "^24.3.0",
38+
"typescript": "^3.7.4"
39+
},
40+
"scripts": {
41+
"build": "NODE_ENV=production run-s clean ts:build",
42+
"ci": "run-s ts eslint prettier test:coverage:report",
43+
"clean": "rm -rf dist/*",
44+
"eslint": "eslint src --cache --ext .ts",
45+
"eslint:fix": "yarn eslint --fix",
46+
"fix": "run-s ts eslint:fix prettier:fix test",
47+
"postversion": "run-s postversion:push:tags postversion:publish postversion:push postversion:echo",
48+
"postversion:echo": "echo \"Successfully released version $npm_package_version!\"",
49+
"postversion:publish": "yarn publish --access public --new-version $npm_package_version",
50+
"postversion:push": "git push",
51+
"postversion:push:tags": "yarn postversion:push --tags",
52+
"prettier": "prettier '**/*.ts' --check",
53+
"prettier:fix": "yarn prettier --write",
54+
"prepare": "npm run build",
55+
"preversion": "run-s fix build",
56+
"test": "jest",
57+
"test:coverage": "jest --coverage || true",
58+
"test:coverage:report": "jest --coverage && cat temp/coverage/lcov.info | coveralls",
59+
"ts": "tsc",
60+
"ts:build": "yarn ts --project tsconfig.build.json"
61+
}
62+
}

Diff for: prettier.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('@wavevision/coding-standard/ts/prettier');

Diff for: src/components/getName.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const getName = <T>(c: import('react').ComponentType<T>): string =>
2+
c.displayName || c.name || 'Component';
3+
4+
export default getName;

Diff for: src/components/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as getName } from './getName';

Diff for: src/components/tests/getName.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import getName from '../getName';
2+
3+
describe('ts-utils/components/getName', () => {
4+
it('returns component displayName', () => {
5+
const Test = (): null => null;
6+
expect(getName(Test)).toBe('Test');
7+
});
8+
it('returns fallback name for unknown component', () => {
9+
expect(getName(() => null)).toBe('Component');
10+
});
11+
});

Diff for: src/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from './components';
2+
export * from './numbers';
3+
export * from './strings';
4+
export * from './type';

Diff for: src/numbers/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export { default as makeId } from './makeId';
2+
export { default as toFixed } from './toFixed';
3+
export { default as toFixedCelled } from './toFixedCelled';
4+
export { default as toFixedRounded } from './toFixedRounded';

Diff for: src/numbers/makeId.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const makeId = (): number => Math.floor(new Date().valueOf() * Math.random());
2+
3+
export default makeId;

Diff for: src/numbers/tests/makeId.test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import makeId from '../makeId';
2+
3+
describe('ts-utils/numbers/makeId', () => {
4+
it('returns random numeric ID', () => {
5+
expect(makeId()).toEqual(expect.any(Number));
6+
});
7+
});

Diff for: src/numbers/tests/toFixed.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import toFixed from '../toFixed';
2+
3+
describe('ts-utils/numbers/toFixed', () => {
4+
it('returns float with fixed decimals without rounding', () => {
5+
expect(toFixed(1.123)).toBe(1.1);
6+
expect(toFixed(5.47826, 3)).toBe(5.478);
7+
});
8+
it('returns input number', () => {
9+
expect(toFixed(22)).toBe(22);
10+
expect(toFixed(0, 0)).toBe(0);
11+
expect(toFixed(Number.NaN)).toBe(Number.NaN);
12+
});
13+
});

Diff for: src/numbers/tests/toFixedCelled.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import toFixedCelled from '../toFixedCelled';
2+
3+
describe('ts-utils/numbers/toFixedCelled', () => {
4+
it('returns float with celled decimals', () => {
5+
expect(toFixedCelled(1.023)).toBe(1.1);
6+
expect(toFixedCelled(2.4465, 3)).toBe(2.447);
7+
});
8+
});

Diff for: src/numbers/tests/toFixedRounded.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import toFixedRounded from '../toFixedRounded';
2+
3+
describe('ts-utils/numbers/toFixedRounded', () => {
4+
it('returns float with rounded decimals', () => {
5+
expect(toFixedRounded(1.123)).toBe(1.1);
6+
expect(toFixedRounded(5.236, 2)).toBe(5.24);
7+
});
8+
});

Diff for: src/numbers/toFixed.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const toFixed = (n: number, decimals = 1): number => {
2+
const exp = new RegExp(`^-?\\d+(?:.\\d{0,${decimals || -1}})?`);
3+
const match = n.toString().match(exp);
4+
return match ? parseFloat(match[0]) : n;
5+
};
6+
7+
export default toFixed;

Diff for: src/numbers/toFixedCelled.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const toFixedCelled = (n: number, decimals = 1): number =>
2+
Number(`${Math.ceil(Number(`${n}e${decimals}`))}e-${decimals}`);
3+
4+
export default toFixedCelled;

Diff for: src/numbers/toFixedRounded.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const toFixedRounded = (n: number, decimals = 1): number =>
2+
Number(n.toFixed(decimals));
3+
4+
export default toFixedRounded;

Diff for: src/strings/camelCaseToDashCase.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const camelCaseToDashCase = (s: string): string =>
2+
s.replace(/([a-zA-Z])(?=[A-Z])/g, '$1-').toLowerCase();
3+
4+
export default camelCaseToDashCase;

Diff for: src/strings/capitalize.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const capitalize = (s: string): string =>
2+
s.charAt(0).toUpperCase() + s.slice(1);
3+
4+
export default capitalize;

Diff for: src/strings/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export { default as camelCaseToDashCase } from './camelCaseToDashCase';
2+
export { default as capitalize } from './capitalize';
3+
export { default as random } from './random';
4+
export { default as shallowStringify } from './shallowStringify';
5+
export { default as splitCamelCase } from './splitCamelCase';

Diff for: src/strings/random.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const random = (length = 5): string =>
2+
(Math.random() + 1).toString(36).substr(2, length);
3+
4+
export default random;

Diff for: src/strings/shallowStringify.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const shallowStringify = (object: unknown): string =>
2+
JSON.stringify(object, (k, v) => (k ? String(v) : v));
3+
4+
export default shallowStringify;

Diff for: src/strings/splitCamelCase.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const splitCamelCase = (s: string): Array<string> =>
2+
s.split(/(?=[A-Z])/).map(s => s.toLowerCase());
3+
4+
export default splitCamelCase;

Diff for: src/strings/tests/camelCaseToDashCase.test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import camelCaseToDashCase from '../camelCaseToDashCase';
2+
3+
describe('ts-utils/strings/camelCaseToDash', () => {
4+
it('converts camelcase to dash', () => {
5+
expect(camelCaseToDashCase('camelCase')).toBe('camel-case');
6+
});
7+
});

Diff for: src/strings/tests/capitalize.test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import capitalize from '../capitalize';
2+
3+
describe('ts-utils/strings/capitalize', () => {
4+
it('returns capitalized string', () => {
5+
expect(capitalize('hello')).toBe('Hello');
6+
});
7+
});

Diff for: src/strings/tests/random.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import random from '../random';
2+
3+
describe('ts-utils/strings/random', () => {
4+
it('generates a random string', () => {
5+
expect(random()).toHaveLength(5);
6+
expect(random(10)).toHaveLength(10);
7+
});
8+
});

Diff for: src/strings/tests/shallowStringify.test.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import shallowStringify from '../shallowStringify';
2+
3+
describe('ts-utils/strings/shallowStringify', () => {
4+
it('returns stringified object', () => {
5+
expect(
6+
shallowStringify({
7+
prop1: 'value',
8+
prop2: true,
9+
prop3: {
10+
nestedProp1: {
11+
nestedProp2: 'value',
12+
},
13+
},
14+
}),
15+
).toBe('{"prop1":"value","prop2":"true","prop3":"[object Object]"}');
16+
});
17+
});

Diff for: src/strings/tests/splitCamelCase.test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import splitCamelCase from '../splitCamelCase';
2+
3+
describe('ts-utils/strings/splitCamelCase', () => {
4+
it('returns array of lower cased parts', () => {
5+
expect(splitCamelCase('someCamelCase')).toEqual(['some', 'camel', 'case']);
6+
});
7+
});

Diff for: src/type/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as isNil } from './isNil';

Diff for: src/type/isNil.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const isNil = (n: unknown): n is null | undefined =>
2+
n === null || n === undefined;
3+
4+
export default isNil;

Diff for: src/type/tests/isNil.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import isNil from '../isNil';
2+
3+
describe('ts-utils/type/isNil', () => {
4+
it('returns true', () => {
5+
expect(isNil(null)).toBe(true);
6+
expect(isNil(undefined)).toBe(true);
7+
});
8+
it('returns false', () => {
9+
expect(isNil(0)).toBe(false);
10+
});
11+
});

Diff for: tsconfig.build.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"declaration": true,
5+
"noEmit": false,
6+
"outDir": "dist"
7+
},
8+
"exclude": ["./src/**/tests/**/*"]
9+
}

Diff for: tsconfig.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "@wavevision/coding-standard/ts/tsconfig.json",
3+
"compilerOptions": {
4+
"allowJs": false,
5+
"module": "commonjs",
6+
"resolveJsonModule": false,
7+
"target": "es5"
8+
},
9+
"include": ["./*.js", "./src/**/*"]
10+
}

0 commit comments

Comments
 (0)