Skip to content

Commit

Permalink
Fix linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ndaidong committed Jun 24, 2024
1 parent cfd0ada commit d6e0cd1
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 166 deletions.
48 changes: 0 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]])`
Expand Down
9 changes: 9 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
Expand Down
39 changes: 16 additions & 23 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -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 };

Expand Down Expand Up @@ -37,29 +31,29 @@ export function copies(
return dest;
}

export const unique = (arr: any[] = []): any[] => {
export const unique = <T>(arr: T[] = []): T[] => {
return [...new Set(arr)];
};

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 = <T>(
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 = <T extends Record<string, any>>(
arr: T[] = [],
order: number = 1,
key: string = "",
): any[] => {
): T[] => {
if (!isString(key) || !hasProperty(arr[0], key)) {
return arr;
}
Expand All @@ -68,9 +62,9 @@ export const sortBy = (
});
};

export const shuffle = (arr: any[] = []): any[] => {
const input: any[] = [...arr];
const output: any[] = [];
export const shuffle = <T>(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);
Expand All @@ -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 = <T>(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);
Expand All @@ -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";
4 changes: 2 additions & 2 deletions tests/detection_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
29 changes: 0 additions & 29 deletions tests/maybe_test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion tests/mod_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assertEquals, assertStringIncludes } from "assert";

import {
AnyObject,
type AnyObject,
clone,
copies,
hasProperty,
Expand Down
12 changes: 6 additions & 6 deletions tests/random_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
});
Expand All @@ -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);
});
Expand Down
14 changes: 10 additions & 4 deletions utils/curry.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
// utils / curry

export const curry = <T extends (...args: any[]) => any>(fn: T) => {
const totalArguments: number = fn.length;
const next = (argumentLength: number, rest: any[]) => {
type AnyFunction = (...args: any[]) => any;

export const curry = <F extends AnyFunction>(
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, []);
};
7 changes: 2 additions & 5 deletions utils/detection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
47 changes: 0 additions & 47 deletions utils/maybe.ts

This file was deleted.

2 changes: 1 addition & 1 deletion utils/string.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit d6e0cd1

Please sign in to comment.