Skip to content

Commit

Permalink
v12.0.1
Browse files Browse the repository at this point in the history
- Stop using native structuredClone
- Improve randint method
  • Loading branch information
ndaidong committed Oct 26, 2024
1 parent 262226c commit 3f19e74
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 16 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ jobs:

strategy:
matrix:
deno-version: [1.44.4]
deno-version: [2.x.x]

steps:
- name: Git Checkout Deno Module
uses: actions/checkout@v4
- name: Use Deno Version ${{ matrix.deno-version }}
uses: denoland/setup-deno@v1
uses: denoland/setup-deno@v2
with:
deno-version: ${{ matrix.deno-version }}
- name: format check
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ndaidong/bellajs",
"version": "12.0.0",
"version": "12.0.1",
"description": "A useful helper for any javascript program",
"homepage": "https://github.com/ndaidong/bellajs",
"repository": {
Expand Down
47 changes: 44 additions & 3 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,52 @@
// mod.ts

import { hasProperty, isArray, isObject, isString } from "./utils/detection.ts";
import { hasProperty, isArray, isObject, isString, isDate } from "./utils/detection.ts";

export type AnyObject = { [key: string]: any };

export const clone = (val: AnyObject): AnyObject => {
return structuredClone(val);
export const clone = (val: any, history: any = null): any => {
const stack = history || new Set()

if (stack.has(val)) {
return val
}

stack.add(val)

if (isDate(val)) {
return new Date(val.valueOf())
}

const copyObject = (o: any): any => {
const oo = Object.create({})
for (const k in o) {
if (hasProperty(o, k)) {
oo[k] = clone(o[k], stack)
}
}
return oo
}

const copyArray = (a: any): any => {
return [...a].map((e: any): any => {
if (isArray(e)) {
return copyArray(e)
} else if (isObject(e)) {
return copyObject(e)
}
return clone(e, stack)
})
}

if (isArray(val)) {
return copyArray(val)
}

if (isObject(val)) {
return copyObject(val)
}

return val
};

export function copies(
Expand Down
22 changes: 15 additions & 7 deletions tests/random_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,21 @@ Deno.test("check if .randint() works correctly", async (t) => {
assertEquals(q, 10);
});

const min = 50;
const max = 80;
await t.step(`.randint() between ${min} - ${max}`, () => {
for (let i = 0; i < 100; i++) {
const q = randint(min, max);
assertEquals(q >= min, true);
assertEquals(q <= max, true);
await t.step(`.randint() in the range of [min, max]`, () => {
for (let i = 0; i < 1000; i++) {
const q = randint(0, 10);
assertEquals(q >= 0, true);
assertEquals(q <= 10, true);
}
for (let i = 0; i < 1000; i++) {
const q = randint(100, 1000);
assertEquals(q >= 100, true);
assertEquals(q <= 1000, true);
}
for (let i = 0; i < 1000; i++) {
const q = randint(0, 10000);
assertEquals(q >= 0, true);
assertEquals(q <= 10000, true);
}
});
});
Expand Down
6 changes: 3 additions & 3 deletions utils/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export const genid = (len: number = 32, prefix: string = ""): string => {
};

export const randint = (min: number = 0, max: number = 1e6): number => {
const byteArray = new Uint8Array(1);
const byteArray = new Uint32Array(1);
crypto.getRandomValues(byteArray);
const floatNum = Number("0." + byteArray[0].toString());
return Math.floor(floatNum * (max - min + 1)) + min;
const randomNumber = byteArray[0] / (0xffffffff + 1);
return Math.floor(randomNumber * (max - min + 1)) + min;
};

0 comments on commit 3f19e74

Please sign in to comment.