diff --git a/.github/workflows/ci-test.yml b/.github/workflows/ci-test.yml index 8644915..2c4c91a 100755 --- a/.github/workflows/ci-test.yml +++ b/.github/workflows/ci-test.yml @@ -7,18 +7,17 @@ on: [push, pull_request] jobs: test: - runs-on: ubuntu-latest 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 diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 102df5a..61206c1 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -2,12 +2,12 @@ name: "CodeQL" on: push: - branches: [ main ] + branches: [main] pull_request: # The branches below must be a subset of the branches above - branches: [ main ] + branches: [main] schedule: - - cron: '33 18 * * 3' + - cron: "33 18 * * 3" jobs: analyze: @@ -21,39 +21,39 @@ jobs: strategy: fail-fast: false matrix: - language: [ 'javascript' ] + language: ["javascript"] # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] # Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support steps: - - name: Checkout repository - uses: actions/checkout@v4 - - # Initializes the CodeQL tools for scanning. - - name: Initialize CodeQL - uses: github/codeql-action/init@v2 - with: - languages: ${{ matrix.language }} - # If you wish to specify custom queries, you can do so here or in a config file. - # By default, queries listed here will override any specified in a config file. - # Prefix the list here with "+" to use these queries and those in the config file. - # queries: ./path/to/local/query, your-org/your-repo/queries@main - - # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). - # If this step fails, then you should remove it and run the build manually (see below) - - name: Autobuild - uses: github/codeql-action/autobuild@v2 - - # ℹī¸ Command-line programs to run using the OS shell. - # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun - - # ✏ī¸ If the Autobuild fails above, remove it and uncomment the following three lines - # and modify them (or add more) to build your code if your project - # uses a compiled language - - #- run: | - # make bootstrap - # make release - - - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v2 + - name: Checkout repository + uses: actions/checkout@v4 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v2 + with: + languages: ${{ matrix.language }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + # queries: ./path/to/local/query, your-org/your-repo/queries@main + + # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). + # If this step fails, then you should remove it and run the build manually (see below) + - name: Autobuild + uses: github/codeql-action/autobuild@v2 + + # ℹī¸ Command-line programs to run using the OS shell. + # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun + + # ✏ī¸ If the Autobuild fails above, remove it and uncomment the following three lines + # and modify them (or add more) to build your code if your project + # uses a compiled language + + #- run: | + # make bootstrap + # make release + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v2 diff --git a/README.md b/README.md index 4e45c2f..86912da 100755 --- a/README.md +++ b/README.md @@ -113,12 +113,12 @@ for (let i = 0; i < 5; i++) { ```html ``` diff --git a/deno.json b/deno.json index 697c5b2..4f395aa 100644 --- a/deno.json +++ b/deno.json @@ -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": { diff --git a/mod.ts b/mod.ts index 5f7e1cb..7be2d4c 100644 --- a/mod.ts +++ b/mod.ts @@ -1,11 +1,58 @@ // mod.ts -import { hasProperty, isArray, isObject, isString } from "./utils/detection.ts"; +import { + hasProperty, + isArray, + isDate, + isObject, + isString, +} 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( diff --git a/tests/random_test.ts b/tests/random_test.ts index 9a10e01..03f7a2e5b 100644 --- a/tests/random_test.ts +++ b/tests/random_test.ts @@ -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); } }); }); diff --git a/utils/random.ts b/utils/random.ts index b10f26c..94fb572 100755 --- a/utils/random.ts +++ b/utils/random.ts @@ -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; };