Skip to content

Commit

Permalink
Merge pull request #56 from functional-jslib/issue-30-issue-43-simpli…
Browse files Browse the repository at this point in the history
…fy_slice_usage

Issue 30 issue 43 simplify slice usage
  • Loading branch information
elycruz authored Nov 28, 2021
2 parents 956a042 + f6e1bf3 commit fa00e5f
Show file tree
Hide file tree
Showing 131 changed files with 5,998 additions and 9,493 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
**/node_modules/**/*
**/dist/**/*.js
**/dist/**/*
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"experimentalObjectRestSpread": true
}
},

"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
Expand Down
13,122 changes: 4,703 additions & 8,419 deletions package-lock.json

Large diffs are not rendered by default.

25 changes: 13 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
"private": true,
"devDependencies": {
"@rollup/plugin-node-resolve": "^11.2.0",
"@rollup/plugin-typescript": "^8.2.0",
"@types/jest": "^26.0.21",
"@rollup/plugin-typescript": "^8.3.0",
"@types/jest": "^27.0.2",
"@types/node": "^13.13.47",
"@typescript-eslint/eslint-plugin": "^2.27.0",
"@typescript-eslint/parser": "^2.27.0",
"@typescript-eslint/eslint-plugin": "^5.4.0",
"@typescript-eslint/parser": "^5.4.0",
"del": "^5.1.0",
"eslint": "^6.8.0",
"jest": "^27.0.4",
"jest-cli": "^27.0.4",
"rollup": "^2.50.6",
"eslint": "^8.1.0",
"jest": "^27.3.1",
"jest-cli": "^27.3.1",
"rollup": "^2.58.3",
"rollup-plugin-terser": "^7.0.2",
"ts-jest": "^27.0.2",
"ts-jest": "^27.0.7",
"ts-node": "^9.1.1",
"tslib": "^2.1.0",
"typescript": "^4.2.3"
"tslib": "^2.3.1",
"typescript": "^4.4.4"
},
"scripts": {
"test": "jest -c jest.config.js",
Expand All @@ -28,7 +28,8 @@
"test:fjl-inputfilter": "jest -c jest.config.js --selectProjects fjl-inputfilter",
"test:fjl-validator": "jest -c jest.config.js --selectProjects fjl-validator",
"build": "rollup --config rollup.config.js",
"link-and-build:packages": "node node_scripts/tasks/link-packages.js"
"link-and-build:packages": "node node_scripts/tasks/link-packages.js",
"link:packages": "npm run link-and-build:packages"
},
"repository": {
"type": "git",
Expand Down
5 changes: 5 additions & 0 deletions packages/fjl-inputfilter/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/fjl-inputfilter/src/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Created by Ely on 7/24/2014.
*/
import {
apply, assign, compose, defineEnumProps, isArray, isset,
assign, compose, defineEnumProps, isArray, isset,
isString, Slice, Unary, error as defaultErrorHandler
} from 'fjl';
import {
Expand Down Expand Up @@ -198,7 +198,7 @@ export const
* Runs filters on value (successively).
*/
runFilters = <T = any>(filters: Unary<T, any>[], value: T): T | any => filters && filters.length ?
apply(compose, filters)(value) : value,
compose(...filters)(value) : value,

/**
* Runs filters on value (successively) and returns result wrapped in a promise.
Expand Down
5 changes: 4 additions & 1 deletion packages/fjl-inputfilter/tests/test-Input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
validateIOInput
} from '../src/input';
import {runHasPropTypes} from "./utils";
import {log} from "../../fjl/tests/helpers";

const toSlug = (x: string): string => (x + '').replace(/[^a-z\d\-_]+/gim, '-').toLowerCase(),
trim = (x: string): string => x ? (x + '').trim() : x,
Expand Down Expand Up @@ -91,7 +92,9 @@ describe('#runValidators', function () {
]
.concat(
subsequences('hello')
.map(x => [runValidators(inputOptionObjs[0].validators, breakOnFailure, (x as string[]).join('')), true, 0])
.map(x => {
return [runValidators(inputOptionObjs[0].validators, breakOnFailure, x), true, 0]
})
))
.forEach(([rsltObj, expectedResult, expectedMsgsLen]) => {
const {result, messages} = rsltObj;
Expand Down
88 changes: 45 additions & 43 deletions packages/fjl-inputfilter/tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,50 @@
import {range, apply} from 'fjl';
import {range} from 'fjl';
import {TypeRef} from "../../fjl/src";

export const

genRan = (min, max) => Math.round(Math.random() * max),

genRanChar = (min = 0, max = 0x10FFFF) =>
String.fromCharCode(genRan(min, max)),

genRanStr = (min = 0, max = 100) =>
range(min, max)
.reduce(str => str + genRanChar(min, max), ''),

runHasPropOfType = (Type, propName, [correctValue, incorrectValue], x) => {
test (`it should have an \`${propName}\` property`, () => {
expect(Object.prototype.hasOwnProperty.call(x, propName)).toEqual(true);
});
test (`it should throw an error when setting \`${propName}\` to ${incorrectValue}`, () => {
expect(() => { x[propName] = incorrectValue; }).toThrow(Error);
});
test (`it should set value correctly for \`${propName}\` when value is of correct type`, () => {
x[propName] = correctValue;
expect(x[propName]).toEqual(correctValue);
});
},

runHasPropOfTypeUnWrapped = (Type, propName, [correctValue, incorrectValue], x) => {
expect(Object.prototype.hasOwnProperty.call(x, propName)).toEqual(true);
expect(() => { x[propName] = incorrectValue; }).toThrow(Error);
x[propName] = correctValue;
expect(x[propName]).toEqual(correctValue);
},

runHasPropTypes = (propTypeArgsList, x) =>
propTypeArgsList.forEach(args => {
const _args = args.slice(0);
_args.push(x);
apply(runHasPropOfType, _args);
}),

runHasPropTypesUnWrapped = (propTypeArgsList, x) =>
propTypeArgsList.forEach(args => {
const _args = args.slice(0);
_args.push(x);
apply(runHasPropOfTypeUnWrapped, _args);
})
genRan = (min: number, max: number) => Math.round(Math.random() * max),

genRanChar = (min = 0, max = 0x10FFFF) =>
String.fromCharCode(genRan(min, max)),

genRanStr = (min = 0, max = 100) =>
range(min, max)
.reduce(str => str + genRanChar(min, max), ''),

runHasPropOfType = (Type: TypeRef, propName: string, [correctValue, incorrectValue]: [any, any], x: any): void => {
test(`it should have an \`${propName}\` property`, () => {
expect(Object.prototype.hasOwnProperty.call(x, propName)).toEqual(true);
});
test(`it should throw an error when setting \`${propName}\` to ${incorrectValue}`, () => {
expect(() => {
x[propName] = incorrectValue;
}).toThrow(Error);
});
test(`it should set value correctly for \`${propName}\` when value is of correct type`, () => {
x[propName] = correctValue;
expect(x[propName]).toEqual(correctValue);
});
},

runHasPropOfTypeUnWrapped = (Type, propName, [correctValue, incorrectValue], x) => {
expect(Object.prototype.hasOwnProperty.call(x, propName)).toEqual(true);
expect(() => {
x[propName] = incorrectValue;
}).toThrow(Error);
x[propName] = correctValue;
expect(x[propName]).toEqual(correctValue);
};

type RunHasPropOfType = typeof runHasPropOfType;
type RunHasPropOfTypesUnWrapped = typeof runHasPropOfTypeUnWrapped;
type P = Parameters<RunHasPropOfType>;
type P2 = Parameters<RunHasPropOfTypesUnWrapped>;

export const runHasPropTypes = (propTypeArgsList: [P[0], P[1], P[2]][], x) =>
propTypeArgsList.forEach(args => runHasPropOfType(...(args.concat([x]) as P))),

runHasPropTypesUnWrapped = (propTypeArgsList: [P2[0], P2[1], P2[2]][], x) =>
propTypeArgsList.forEach(args => runHasPropOfTypeUnWrapped(...(args.concat([x])) as P2))

;
4 changes: 2 additions & 2 deletions packages/fjl-validator-recaptcha/tests/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {apply, log, peek, range} from 'fjl';
import {log, peek, range} from 'fjl';

export {log, peek};

Expand Down Expand Up @@ -32,7 +32,7 @@ export const
propTypeArgsList.forEach(args => {
const _args = args.slice(0);
_args.push(x);
apply(runHasPropOfType, _args);
runHasPropOfType(..._args);
})

;
5 changes: 5 additions & 0 deletions packages/fjl-validator/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/fjl-validator/tests/test-stringLengthValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('#toStringLengthOptions', function () {
describe('#stringLengthValidator', function () {
const testArgs = (subsequences('hello')
.concat([repeat(21, 'a'), repeat(14, 'b')]) as string[][])
.map(x => x.join(''));
.map(x => x instanceof Array ? x.join('') : x);
test('should return a validation result object with a `result` of `false` and ' +
'one error message when value is not of type `String`', function () {
const strValidator = stringLengthValidator(null);
Expand Down
46 changes: 46 additions & 0 deletions packages/fjl/designs/curry.designs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

## Why "unidiomatic" curry is difficult (in typescript):

### Reason 1:
Types for the following (etc.) are required:

#### `curry2`
```
(a, b) -> c
a -> b -> c
```

#### `curry3`
```
(a, b, c) -> d
(a, b) -> c -> d
a -> b -> c -> d
a -> (b, c) -> d
```

#### `curry4`:

```
(a, b, c, d) -> e
(a, b, c) -> d -> e
(a, b) -> c -> d -> e
a -> b -> c -> d -> e
a -> b -> (c, d) -> e
a -> (b, c, d) -> e
(a, b) -> (c, d) -> e
```

#### `curry5`:

```
(a, b, c, d, e) -> f
(a, b, c, d) -> e -> f
(a, b, c) -> d -> e -> f
(a, b) -> c -> d -> e -> f
a -> b -> c -> d -> e -> f
a -> b -> c -> (d, e) -> f
a -> b -> (c, d, e) -> f
a -> (b, c, d, e) -> f
(a, b) -> (c, d, e) -> f
(a, b, c) -> (d, e) -> f
```
5 changes: 5 additions & 0 deletions packages/fjl/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions packages/fjl/src/boolean/equal.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {curry2, CurryPred2} from '../function/curry';

/**
* Equality combinator.
*/
export const

equal = <T>(a: T, b: T): boolean => a === b,
import {curry2, CurryOf2} from "../function";

$equal = curry2(equal) as CurryPred2<any>;
export const equal = <T = any>(a: T, b: T): boolean => a === b;
export type Equal = typeof equal;
export type EqualParams = Parameters<Equal>;
export const $equal = curry2(equal) as CurryOf2<EqualParams[0], EqualParams[1], ReturnType<Equal>>;
2 changes: 1 addition & 1 deletion packages/fjl/src/boolean/isFalsy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
/**
* Returns whether `value` is 'falsy' or not
*/
export const isFalsy = <T>(value: T): boolean => !value;
export const isFalsy = (value: any): boolean => !value;

2 changes: 1 addition & 1 deletion packages/fjl/src/boolean/isTruthy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/**
* Returns whether `value` is 'truthy' or not.
*/
export const isTruthy = <T>(value: T): boolean => !!value;
export const isTruthy = (value: any): boolean => !!value;
21 changes: 12 additions & 9 deletions packages/fjl/src/data/either.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* Created by elyde on 12/10/2016.
*/
import {isset} from '../object/isset';
import {curry} from '../function/curry';
import {id} from '../function/id';
import {MonadBase} from './monad';
import {FunctorMapFn} from "../types";
import {curry3, CurryOf3} from "../function";

export type Either<A, B> = A | B;
export type Either<A, B> = Right<A> | Right<B>;

/**
* `Left` representation of `Either` construct.
Expand Down Expand Up @@ -79,19 +79,22 @@ export const
* Converts given to an either (`Right`|`Left`)
*/
toEither = <A, B>(x: A): Either<Right<A>, Left<B>> =>
(isLeft(x) || isRight(x) ? x : right(x).map(id)) as Either<Right<A>, Left<B>>,
(isLeft(x) || isRight(x) ? x : (!isset(x) ? left(x) : right(x).map(id))) as Either<Right<A>, Left<B>>,

/**
* Calls matching callback on incoming `Either`; If it's an `Left` type, calls left-callback on it,
* If it's an `Right` type, calls the right-callback on it.
* Returns value of the flat-mapped monad.
*/
either = <A, B, C>(leftCallback: FunctorMapFn<C>, rightCallback: FunctorMapFn<C>, _either_: Left<A> | Right<B>): C =>
_either_.map(isRight(_either_) ? rightCallback : leftCallback).join(),
_either_.map(isRight(_either_) ? rightCallback : leftCallback).join();

/**
* Curried version `either`.
*/
$either = curry(either)
export type EitherFn = typeof either;

export type EitherFnParams = Parameters<EitherFn>;

;
/**
* Curried version `either`.
*/
export const $either =
curry3(either) as CurryOf3<EitherFnParams[0], EitherFnParams[1], EitherFnParams[2], ReturnType<EitherFn>>;
Loading

0 comments on commit fa00e5f

Please sign in to comment.