Skip to content

Commit

Permalink
Merge pull request #139 from ndaidong/dev
Browse files Browse the repository at this point in the history
v12.0.1
  • Loading branch information
ndaidong authored Oct 26, 2024
2 parents 262226c + 3619674 commit 3a05fa5
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 57 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/ci-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 35 additions & 35 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ for (let i = 0; i < 5; i++) {

```html
<script type="module">
import { genid } from "https://esm.sh/@ndaidong/bellajs";
// import { genid } from 'https://unpkg.com/@ndaidong/bellajs/esm/mod.js';
import { genid } from "https://esm.sh/@ndaidong/bellajs";
// import { genid } from 'https://unpkg.com/@ndaidong/bellajs/esm/mod.js';
for (let i = 0; i < 5; i++) {
console.log(genid());
}
for (let i = 0; i < 5; i++) {
console.log(genid());
}
</script>
```

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
53 changes: 50 additions & 3 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -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(
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 3a05fa5

Please sign in to comment.