Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: functions for Math object and number methods #1

Merged
merged 8 commits into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
{
"name": "unstandard",
"version": "0.0.2",
"description": "A point-free library for the standard objects",
"name": "unprimitive",
"version": "0.0.1",
"description": "A point-free library for the primitive data",
"main": "lib/index.js",
"license": "MIT",
"author": "Allan Hortle",
"repository": {
"type": "git",
"url": "git+https://github.com/blueflag/unstandard.git"
"url": "git+https://github.com/blueflag/unprimitive.git"
},
"files": [
"lib"
],
"bugs": {
"url": "https://github.com/blueflag/unstandard/issues"
"url": "https://github.com/blueflag/unprimitive/issues"
},
"private": false,
"scripts": {
"prepublishOnly": "yarn build",
"build": "rm -rf lib && NODE_ENV=production babel src --out-dir lib --ignore **/*-test.js",
"watch": "yarn build -w"
"watch": "yarn build -w",
"test": "yarn jest"
},
"devDependencies": {
"babel-cli": "^6.23.0",
"babel-core": "^6.23.0",
"babel-preset-blueflag": "^0.6.0",
"babel-plugin-extract-flow-types": "^1.0.0",
"blueflag-test": "^0.20.0"
},
"dependencies": {
"babel-plugin-extract-flow-types": "^1.0.0"
}
}
85 changes: 85 additions & 0 deletions src/__tests__/math-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//@flow
import * as method from '../math/method';
import * as standard from '../math/standard';

jest.spyOn(Math, 'random');

// Methods
test.each([
['toExponential', 1, 3, '1.000e+0'],
['toFixed', 1.11111, 2, '1.11'],
['toPrecision', 1.11111, 2, '1.1'],
['toString', 15, 16, 'f'],
['toLocaleString', 1.11111, 'arab', '1.111'],
])(
'.%s()',
(name, aa, bb, expected) => {
expect(method[name](bb)(aa)).toBe(expected);
}
);

// Standard
test.each([
['abs', -1, undefined, 1],
['acos', 0.5, undefined, 1.0471975511965979],
['acosh', 2, undefined, 1.3169578969248166],
['asin', 1, , 1.5707963267948966],
['asinh', 1, , 0.881373587019543],
['atan', 1, , 0.7853981633974483],
['atanh', .5, , 0.5493061443340548],
['atan2', 90, 45, 1.1071487177940904],
['ceil', 1.2, , 2],
['cbrt', 2, , 1.2599210498948732],
['expm1', -1, , -0.6321205588285577],
['clz32', 1000, , 22],
['cos', 1, , 0.5403023058681398],
['cosh', 1, , 1.5430806348152437],
['exp', -1, , 0.36787944117144233],
['floor', 1.3333, , 1],
['fround', 1.337, , 1.3370000123977661],
['hypot', 3, 4, 5],
['imul', 0xfffffffe, 5, -10],
['log', 10, , 2.302585092994046],
['log1p', 1, , 0.6931471805599453],
['log2', 3, , 1.584962500721156],
['log10', 2, , 0.3010299956639812],
['max', 10, 2, 10],
['min', 2, 10, 2],
['pow', 2, 10, 1024],
//['random', , , Math.random()],
['round', -20.5, , -20],
['sign', -3, , -1],
['sin', 1, , 0.8414709848078965],
['sinh', 1, , 1.1752011936438014],
['sqrt', 9, , 3],
['tan', 1, , 1.5574077246549023],
['tanh', 1, , 0.7615941559557649],
['trunc', 42.1231, , 42],
])(
'Math.%s()',
(name, aa, bb, expected) => {
expect(standard[name](bb)(aa)).toBe(expected);
}
);

test('random()', () => {
standard.random()();
expect(Math.random).toHaveBeenCalled();
})

// Constants
test.each([
['E', Math.E],
['LN10', Math.LN10],
['LN2', Math.LN2],
['LOG10E', Math.LOG10E],
['LOG2E', Math.LOG2E],
['PI', Math.PI],
['SQRT1_2', Math.SQRT1_2],
['SQRT2', Math.SQRT2]
])(
'Math.%s',
(name, expected) => {
expect(standard[name]()).toBe(expected);
}
);
16 changes: 8 additions & 8 deletions src/__tests__/operator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,22 @@ describe('equals', () => {


test('notEquals', () => {
expect(notEquals(3)(2)).toBe(3 !== 2);
expect(notEquals(true)(false)).toBeTruthy();
expect(notEquals(3)(2)).toBe(true);
expect(notEquals(true)(false)).toBe(true);
});

test('abstractEquals', () => {
expect(abstractEquals(3)(3)).toBeTruthy();
expect(abstractEquals(true)('true')).toBeTruthy();
expect(abstractEquals(3)(3)).toBe(true);
expect(abstractEquals(true)(1)).toBe(true);
});

test('abstractNotEquals', () => {
expect(abstractNotEquals(2)(3)).toBeTruthy();
expect(abstractNotEquals(false)('true')).toBeTruthy();
expect(abstractNotEquals(2)(3)).toBe(true);
expect(abstractNotEquals(false)(1)).toBe(true);
});

test('gt', () => expect(gt(2)(3)).toBeTruthy());
test('lt', () => expect(lt(3)(2)).toBeTruthy());
test('gt', () => expect(gt(2)(3)).toBe(true));
test('lt', () => expect(lt(3)(2)).toBe(true));

//gte
test.each([
Expand Down
61 changes: 61 additions & 0 deletions src/__tests__/string-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//@flow
import * as method from '../string/method';
import * as standard from '../string/standard';

// Methods
test.each([
['charAt', 1, 'foo', 'o'],
['charCodeAt', 1, 'foo', 111],
['codePointAt', 0, 'f', 102],
['concat', 'foo', 'bar', 'barfoo'],
['endsWith', 'o', 'foo', true],
['includes', 'foo', 'foobar', true],
['indexOf', 'o', 'foo', 1],
['lastIndexOf', 'o', 'fool', 2],
['localeCompare', 'bar', 'foo', 1],
['normalize', 'NFKC', '\u1E9B\u0323', '\u1E69'],
['padEnd', [6, '.'], 'foo', 'foo...'],
['padStart', [6, '.'], 'foo', '...foo'],
['repeat', 10, 'x', 'xxxxxxxxxx'],
['replace', ['foo', 'bar'], '--foo--', '--bar--'],
['search', 'foo', '--foo--', 2],
['slice', [1,4], 'radical', 'adi'],
['split', '.', 'a.b.c.d.e', ['a', 'b', 'c', 'd', 'e']],
['substring', [4, 6], 'foobarbaz', 'ar'],
['startsWith', 'foo bar', 'foo bar baz', true],
['toString', , new String('foo'), 'foo'],
['valueOf', , new String('foo'), 'foo'],
['trim', , ' foo ', 'foo'],
['trimLeft', , ' foo', 'foo'],
['trimRight', , 'foo ', 'foo'],
['toLocaleLowerCase', , 'GESÄSS', 'gesäss'],
['toLocaleUpperCase', , 'Gesäß', 'GESÄSS'],
['toLowerCase', , 'FOO', 'foo'],
['toUpperCase', , 'foo', 'FOO'],
])(
'%s(%p)(%p) === %p',
(name, bb, aa, expected) => {
const first = Array.isArray(bb)
? method[name](...bb)
: method[name](bb)
;

expect(first(aa)).toEqual(expected);
}
);

test('match', () => {
expect(method.match(/foo(bar)/)('foobar')[1]).toBe('bar');
});

// Standard
test.each([
['fromCharCode', 65, , 'A'],
['fromCodePoint', 194564, , '你']
])(
'String.%s()',
(name, aa, bb, expected) => {
expect(standard[name](bb)(aa)).toBe(expected);
}
);

6 changes: 6 additions & 0 deletions src/math/method.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @flow
export const toExponential = (aa: number) => (bb: number) => bb.toExponential(aa);
export const toFixed = (aa: number) => (bb: number) => bb.toFixed(aa);
export const toPrecision = (aa: number) => (bb: number) => bb.toPrecision(aa);
export const toString = (aa: number) => (bb: number) => bb.toString(aa);
export const toLocaleString = () => (bb: number) => bb.toLocaleString();
46 changes: 46 additions & 0 deletions src/math/standard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
export const abs = () => (aa) => Math.abs(aa);
export const acos = () => (aa) => Math.acos(aa);
export const acosh = () => (aa) => Math.acosh(aa);
export const asin = () => (aa) => Math.asin(aa);
export const asinh = () => (aa) => Math.asinh(aa);
export const atan = () => (aa) => Math.atan(aa);
export const atanh = () => (aa) => Math.atanh(aa);
export const atan2 = (bb) => (aa) => Math.atan2(aa, bb); //2
export const ceil = () => (aa) => Math.ceil(aa);
export const cbrt = () => (aa) => Math.cbrt(aa);
export const expm1 = () => (aa) => Math.expm1(aa);
export const clz32 = () => (aa) => Math.clz32(aa);
export const cos = () => (aa) => Math.cos(aa);
export const cosh = () => (aa) => Math.cosh(aa);
export const exp = () => (aa) => Math.exp(aa);
export const floor = () => (aa) => Math.floor(aa);
export const fround = () => (aa) => Math.fround(aa);
export const hypot = (...rest) => (aa) => Math.hypot(aa, ...rest); //2+
export const imul = (bb) => (aa) => Math.imul(aa, bb); //2
export const log = () => (aa) => Math.log(aa);
export const log1p = () => (aa) => Math.log1p(aa);
export const log2 = () => (aa) => Math.log2(aa);
export const log10 = () => (aa) => Math.log10(aa);
export const max = (...rest) => (aa) => Math.max(aa, ...rest); //2
export const min = (...rest) => (aa) => Math.min(aa, ...rest); //2
export const pow = (bb) => (aa) => Math.pow(aa, bb); //2
export const random = () => (aa) => Math.random(aa);
export const round = () => (aa) => Math.round(aa);
export const sign = () => (aa) => Math.sign(aa);
export const sin = () => (aa) => Math.sin(aa);
export const sinh = () => (aa) => Math.sinh(aa);
export const sqrt = () => (aa) => Math.sqrt(aa);
export const tan = () => (aa) => Math.tan(aa);
export const tanh = () => (aa) => Math.tanh(aa);
export const trunc = () => (aa) => Math.trunc(aa);


// Constants
export const E = () => Math.E;
export const LN10 = () => Math.LN10;
export const LN2 = () => Math.LN2;
export const LOG10E = () => Math.LOG10E;
export const LOG2E = () => Math.LOG2E;
export const PI = () => Math.PI;
export const SQRT1_2 = () => Math.SQRT1_2;
export const SQRT2 = () => Math.SQRT2;
4 changes: 2 additions & 2 deletions src/operator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export const divide = (aa: number) => (bb: number): number => bb / aa;
export const power = (aa: number) => (bb: number): number => bb ** aa;
export const equals = (aa: any) => (bb: any): any => bb === aa;
export const notEquals = (aa: any) => (bb: any): any => bb !== aa;
export const abstractEquals = (aa: any) => (bb: any): any => aa + bb;
export const abstractNotEquals = (aa: any) => (bb: any): any => aa + bb;
export const abstractEquals = (aa: any) => (bb: any): any => aa == bb;
export const abstractNotEquals = (aa: any) => (bb: any): any => aa != bb;
export const gt = (aa: number) => (bb: number): boolean => bb > aa;
export const gte = (aa: number) => (bb: number): boolean => bb >= aa;
export const lt = (aa: number) => (bb: number): boolean => bb < aa;
Expand Down
30 changes: 30 additions & 0 deletions src/string/method.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// @flow
export const charAt = (bb: number) => (aa: string) => aa.charAt(bb);
export const charCodeAt = (bb: number) => (aa: string) => aa.charCodeAt(bb);
export const codePointAt = (bb: number) => (aa: string) => aa.codePointAt(bb);
export const concat = (bb: string) => (aa: string) => aa.concat(bb);
export const endsWith = (bb: string) => (aa: string) => aa.endsWith(bb);
export const includes = (bb: string) => (aa: string) => aa.includes(bb);
export const indexOf = (bb: string) => (aa: string) => aa.indexOf(bb);
export const lastIndexOf = (bb: string) => (aa: string) => aa.lastIndexOf(bb);
export const localeCompare = (bb: string) => (aa: string) => aa.localeCompare(bb);
export const match = (bb: RegExp) => (aa: string) => aa.match(bb);
export const normalize = (bb: string) => (aa: string) => aa.normalize(bb);
export const padEnd = (length: number, padString?: string) => (aa: string) => aa.padEnd(length, padString);
export const padStart = (length: number, padString?: string) => (aa: string) => aa.padStart(length, padString);
export const repeat = (bb: number) => (aa: string) => aa.repeat(bb);
export const replace = (match: string|RegExp, replace: string|Function) => (aa: string) => aa.replace(match, replace);
export const search = (bb: string) => (aa: string) => aa.search(bb);
export const slice = (begin: number, end?: number) => (aa: string) => aa.slice(begin, end);
export const split = (bb: string) => (aa: string) => aa.split(bb);
export const substring = (begin: number, end?: number) => (aa: string) => aa.substring(begin, end);
export const startsWith = (bb: string) => (aa: string) => aa.startsWith(bb);
export const toString = () => (aa: string) => aa.toString();
export const trim = () => (aa: string) => aa.trim();
export const trimLeft = () => (aa: string) => aa.trimLeft();
export const trimRight = () => (aa: string) => aa.trimRight();
export const toLocaleLowerCase = () => (aa: string) => aa.toLocaleLowerCase();
export const toLocaleUpperCase = () => (aa: string) => aa.toLocaleUpperCase();
export const toLowerCase = () => (aa: string) => aa.toLowerCase();
export const toUpperCase = () => (aa: string) => aa.toUpperCase();
export const valueOf = () => (aa: string) => aa.valueOf();
3 changes: 3 additions & 0 deletions src/string/standard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @flow
export const fromCharCode = () => (aa: number) => String.fromCharCode(aa);
export const fromCodePoint = () => (aa: number) => String.fromCodePoint(aa);