diff --git a/README.md b/README.md index 11ac483..4e45c2f 100755 --- a/README.md +++ b/README.md @@ -23,7 +23,6 @@ apps. [`shuffle`](#shufflearray-arr), [`unique`](#uniquearray-arr) - [Functional utils](#functional-utils): [`curry`](#curryfn), [`compose`](#composef1-f2-fn), [`pipe`](#pipef1-f2-fn), - [`maybe`](#maybeanything-val) - [Date utils](#date-utils): [`formatDateString`](#formatdatestringdate--timestamp--string-locale--object-options), [`formatTimeAgo`](#formattimeagodate--timestamp--string-locale--string-justnow) @@ -431,53 +430,6 @@ add1AndMult2(3); // => 8 // because add 1 first, then multiple to 2 late => (3 + 1) * 2 ``` -#### `maybe(Anything val)` - -Return a static variant of `Maybe` monad. - -```js -import { maybe } from "@ndaidong/bellajs"; - -const plus5 = (x) => x + 5; -const minus2 = (x) => x - 2; -const isNumber = (x) => Number(x) === x; -const toString = (x) => "The value is " + String(x); -const getDefault = () => "This is default value"; - -maybe(5) - .map(plus5) - .map(minus2) - .value(); // 8 - -maybe("noop") - .map(plus5) - .map(minus2) - .value(); // null - -maybe(5) - .if(isNumber) - .map(plus5) - .map(minus2) - .else(getDefault) - .map(toString) - .value(); // 'The value is 8' - -maybe() - .if(isNumber) - .map(plus5) - .map(minus2) - .map(toString) - .value(); // null - -maybe() - .if(isNumber) - .map(plus5) - .map(minus2) - .else(getDefault) - .map(toString) - .value(); // 'This is default value' -``` - ### Date utils #### `formatDateString(Date | Timestamp [, String locale [, Object options]])` diff --git a/deno.json b/deno.json index ef529f3..ef18c82 100644 --- a/deno.json +++ b/deno.json @@ -17,6 +17,15 @@ "@deno/dnt": "jsr:@deno/dnt@^0.41.2" }, "exports": "./mod.ts", + "lint": { + "include": ["mod.ts", "utils/*.ts", "scripts/*.ts", "tests/*.ts"], + "exclude": ["npm"], + "rules": { + "tags": ["recommended"], + "include": [], + "exclude": ["no-explicit-any"] + } + }, "test": { "include": ["tests"], "exclude": [] diff --git a/mod.ts b/mod.ts index ce76340..5f7e1cb 100644 --- a/mod.ts +++ b/mod.ts @@ -1,12 +1,6 @@ // mod.ts -import { - hasProperty, - isArray, - isDate, - isObject, - isString, -} from "./utils/detection.ts"; +import { hasProperty, isArray, isObject, isString } from "./utils/detection.ts"; export type AnyObject = { [key: string]: any }; @@ -37,7 +31,7 @@ export function copies( return dest; } -export const unique = (arr: any[] = []): any[] => { +export const unique = (arr: T[] = []): T[] => { return [...new Set(arr)]; }; @@ -45,21 +39,21 @@ const fnSort = (a: any, b: any): number => { return a > b ? 1 : (a < b ? -1 : 0); }; -export const sort = ( - arr: any[] = [], - sorting: ((a: any, b: any) => number) | null = null, -): any[] => { - const tmp: any[] = [...arr]; - const fn: (a: any, b: any) => number = sorting || fnSort; +export const sort = ( + arr: T[] = [], + sorting: ((a: T, b: T) => number) | null = null, +): T[] => { + const tmp: T[] = [...arr]; + const fn: (a: T, b: T) => number = sorting || fnSort; tmp.sort(fn); return tmp; }; -export const sortBy = ( - arr: any[] = [], +export const sortBy = >( + arr: T[] = [], order: number = 1, key: string = "", -): any[] => { +): T[] => { if (!isString(key) || !hasProperty(arr[0], key)) { return arr; } @@ -68,9 +62,9 @@ export const sortBy = ( }); }; -export const shuffle = (arr: any[] = []): any[] => { - const input: any[] = [...arr]; - const output: any[] = []; +export const shuffle = (arr: T[] = []): T[] => { + const input: T[] = [...arr]; + const output: T[] = []; let inputLen: number = input.length; while (inputLen > 0) { const index: number = Math.floor(Math.random() * inputLen); @@ -80,8 +74,8 @@ export const shuffle = (arr: any[] = []): any[] => { return output; }; -export const pick = (arr: any[] = [], count: number = 1): any[] => { - const a: any[] = shuffle(arr); +export const pick = (arr: T[] = [], count: number = 1): T[] => { + const a: T[] = shuffle(arr); const mc: number = Math.max(1, count); const c: number = Math.min(mc, a.length - 1); return a.splice(0, c); @@ -95,4 +89,3 @@ export * from "./utils/date.ts"; export * from "./utils/curry.ts"; export * from "./utils/compose.ts"; export * from "./utils/pipe.ts"; -export * from "./utils/maybe.ts"; diff --git a/tests/detection_test.ts b/tests/detection_test.ts index e8f7de0..c3f27ce 100644 --- a/tests/detection_test.ts +++ b/tests/detection_test.ts @@ -50,7 +50,7 @@ Deno.test("check if .isInteger() works correctly", async (t) => { }); Deno.test("check if .isArray() works correctly", async (t) => { - const positives = [[], [1, 2, 3], new Array(), Array.from(new Set())]; + const positives = [[], [1, 2, 3], Array.from(new Set())]; for (const val of positives) { await t.step(`test .isArray(${val}) --> true`, () => { assertEquals(isArray(val), true); @@ -241,7 +241,7 @@ Deno.test("check if .hasProperty() works correctly", async (t) => { }); } - const negatives = [{ a: 1 }, "email", 9, "__proto__"]; + const negatives = ["email", "__proto__"]; for (const val of negatives) { await t.step(`test .hasProperty(${val}) --> true`, () => { assertEquals(hasProperty(obj, val), false); diff --git a/tests/maybe_test.ts b/tests/maybe_test.ts deleted file mode 100644 index f9b2ab8..0000000 --- a/tests/maybe_test.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { assertEquals } from "assert"; - -import { maybe } from "../utils/maybe.ts"; - -Deno.test("check if .maybe() works correctly", () => { - const plus5 = (x: number): number => x + 5; - const minus2 = (x: number): number => x - 2; - const isNumber = (x: any): boolean => Number(x) === x; - const toString = (x: string): string => "The value is " + String(x); - const getDefault = (): string => "This is default value"; - - const x1 = maybe(5) - .if(isNumber) - .map(plus5) - .map(minus2) - .map(toString) - .else(getDefault) - .value(); - assertEquals(x1, "The value is 8"); - - const x2 = maybe("nothing") - .if(isNumber) - .map(plus5) - .map(minus2) - .map(toString) - .else(getDefault) - .value(); - assertEquals(x2, "This is default value"); -}); diff --git a/tests/mod_test.ts b/tests/mod_test.ts index 0fecba4..b89df34 100644 --- a/tests/mod_test.ts +++ b/tests/mod_test.ts @@ -1,7 +1,7 @@ import { assertEquals, assertStringIncludes } from "assert"; import { - AnyObject, + type AnyObject, clone, copies, hasProperty, diff --git a/tests/random_test.ts b/tests/random_test.ts index 2e0797b..9a10e01 100644 --- a/tests/random_test.ts +++ b/tests/random_test.ts @@ -8,20 +8,20 @@ Deno.test("check if .randint() works correctly", async (t) => { randArr.push(randint()); } - await t.step(`.randint() after ${randArr.length} times`, async () => { + await t.step(`.randint() after ${randArr.length} times`, () => { assertEquals(randArr.length, 20); const uniqVal = Array.from(new Set(randArr)); assertEquals(uniqVal.length > 10, true); }); - await t.step(".randint() with min = max", async () => { + await t.step(".randint() with min = max", () => { const q = randint(10, 10); assertEquals(q, 10); }); const min = 50; const max = 80; - await t.step(`.randint() between ${min} - ${max}`, async () => { + await t.step(`.randint() between ${min} - ${max}`, () => { for (let i = 0; i < 100; i++) { const q = randint(min, max); assertEquals(q >= min, true); @@ -36,12 +36,12 @@ Deno.test("check if .genid() works correctly", async (t) => { randArr.push(randint()); } - await t.step(".genid() default param", async () => { + await t.step(".genid() default param", () => { const actual = genid(); assertEquals(actual.length, 32); }); - await t.step(".genid(512) default param", async () => { + await t.step(".genid(512) default param", () => { const actual = genid(512); assertEquals(actual.length, 512); }); @@ -53,7 +53,7 @@ Deno.test("check if .genid() works correctly", async (t) => { } const uniques = Array.from(new Set(ids)); - await t.step(`.genid() always return unique string`, async () => { + await t.step(`.genid() always return unique string`, () => { assertEquals(ids.length, len); assertEquals(uniques.length, len); }); diff --git a/utils/curry.ts b/utils/curry.ts index 3bb349f..eaa7225 100755 --- a/utils/curry.ts +++ b/utils/curry.ts @@ -1,14 +1,20 @@ // utils / curry -export const curry = any>(fn: T) => { - const totalArguments: number = fn.length; - const next = (argumentLength: number, rest: any[]) => { +type AnyFunction = (...args: any[]) => any; + +export const curry = ( + fn: F, +): (...args: any[]) => any => { + const totalArguments = fn.length; + + const next = (argumentLength: number, rest: any[]): any => { if (argumentLength > 0) { - return (...args: any[]) => { + return (...args: any[]): any => { return next(argumentLength - args.length, [...rest, ...args]); }; } return fn(...rest); }; + return next(totalArguments, []); }; diff --git a/utils/detection.ts b/utils/detection.ts index 42b0e7c..40c82b6 100755 --- a/utils/detection.ts +++ b/utils/detection.ts @@ -61,9 +61,6 @@ export const isEmpty = (val: any): boolean => { (isObject(val) && Object.keys(val).length === 0); }; -export const hasProperty = (ob: any, k: any): boolean => { - if (!ob || !k) { - return false; - } - return Object.prototype.hasOwnProperty.call(ob, k); +export const hasProperty = (obj: any, prop: string): boolean => { + return Object.prototype.hasOwnProperty.call(obj, prop); }; diff --git a/utils/maybe.ts b/utils/maybe.ts deleted file mode 100755 index 5465f58..0000000 --- a/utils/maybe.ts +++ /dev/null @@ -1,47 +0,0 @@ -// utils / maybe - -import { defineProp } from "./defineProp.ts"; - -export const maybe = (val: T) => { - const __val = val; - const isNil = (): boolean => { - return __val === null || __val === undefined; - }; - const value = (): T => { - return __val; - }; - const getElse = (fn: () => T): typeof maybe => { - return maybe(__val || fn()); - }; - const filter = (fn: (val: T) => boolean): typeof maybe => { - return maybe(fn(__val) === true ? __val : null); - }; - const map = (fn: (val: T) => U): typeof maybe => { - return maybe(isNil() ? null : fn(__val)); - }; - const output = Object.create({}); - Object.defineProperty(output, "__value__", { - value: __val, - enumerable: true, - }); - Object.defineProperty(output, "__type__", { - value: "Maybe", - enumerable: true, - }); - Object.defineProperty(output, "isNil", { - value: isNil, - }); - Object.defineProperty(output, "value", { - value: value, - }); - Object.defineProperty(output, "map", { - value: map, - }); - Object.defineProperty(output, "if", { - value: filter, - }); - Object.defineProperty(output, "else", { - value: getElse, - }); - return output; -}; diff --git a/utils/string.ts b/utils/string.ts index 8217012..64f7f97 100755 --- a/utils/string.ts +++ b/utils/string.ts @@ -1,6 +1,6 @@ // utils / string -import { hasProperty, isArray, isNumber, isString } from "./detection.ts"; +import { hasProperty, isString } from "./detection.ts"; const toString = (input: any): string => { return !isString(input) ? String(input) : input;