@jrc03c/pause 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build.mjs ADDED
@@ -0,0 +1,25 @@
1
+ import { execSync } from "node:child_process"
2
+
3
+ function rebuild() {
4
+ console.log("-----")
5
+ console.log(`\nRebuilding... (${new Date().toLocaleString()})`)
6
+
7
+ try {
8
+ const baseCommand = "npx esbuild src/index.mjs --bundle"
9
+
10
+ const commands = [
11
+ `${baseCommand} --platform=node --outfile=dist/pause.require.cjs`,
12
+ `${baseCommand} --outfile=dist/pause.standalone.cjs`,
13
+ ]
14
+
15
+ commands.forEach(command => {
16
+ execSync(command, { encoding: "utf8" })
17
+ })
18
+
19
+ console.log("\nDone! 🎉\n")
20
+ } catch (e) {
21
+ console.error(e)
22
+ }
23
+ }
24
+
25
+ rebuild()
@@ -0,0 +1,55 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/index.mjs
20
+ var src_exports = {};
21
+ __export(src_exports, {
22
+ pause: () => pauseAsync,
23
+ pauseAsync: () => pauseAsync,
24
+ pauseSync: () => pauseSync
25
+ });
26
+ module.exports = __toCommonJS(src_exports);
27
+ function pauseAsync(ms) {
28
+ return new Promise((resolve, reject) => {
29
+ try {
30
+ const start = /* @__PURE__ */ new Date();
31
+ return setTimeout(() => resolve(/* @__PURE__ */ new Date() - start), ms);
32
+ } catch (e) {
33
+ return reject(e);
34
+ }
35
+ });
36
+ }
37
+ function pauseSync(ms) {
38
+ const start = /* @__PURE__ */ new Date();
39
+ let now = /* @__PURE__ */ new Date();
40
+ while (now - start < ms) {
41
+ now = /* @__PURE__ */ new Date();
42
+ }
43
+ return /* @__PURE__ */ new Date() - start;
44
+ }
45
+ if (typeof window !== "undefined") {
46
+ window.pause = pauseAsync;
47
+ window.pauseAsync = pauseAsync;
48
+ window.pauseSync = pauseSync;
49
+ }
50
+ // Annotate the CommonJS export names for ESM import in node:
51
+ 0 && (module.exports = {
52
+ pause,
53
+ pauseAsync,
54
+ pauseSync
55
+ });
@@ -0,0 +1,26 @@
1
+ (() => {
2
+ // src/index.mjs
3
+ function pauseAsync(ms) {
4
+ return new Promise((resolve, reject) => {
5
+ try {
6
+ const start = /* @__PURE__ */ new Date();
7
+ return setTimeout(() => resolve(/* @__PURE__ */ new Date() - start), ms);
8
+ } catch (e) {
9
+ return reject(e);
10
+ }
11
+ });
12
+ }
13
+ function pauseSync(ms) {
14
+ const start = /* @__PURE__ */ new Date();
15
+ let now = /* @__PURE__ */ new Date();
16
+ while (now - start < ms) {
17
+ now = /* @__PURE__ */ new Date();
18
+ }
19
+ return /* @__PURE__ */ new Date() - start;
20
+ }
21
+ if (typeof window !== "undefined") {
22
+ window.pause = pauseAsync;
23
+ window.pauseAsync = pauseAsync;
24
+ window.pauseSync = pauseSync;
25
+ }
26
+ })();
@@ -0,0 +1,8 @@
1
+ import globals from "globals";
2
+ import pluginJs from "@eslint/js";
3
+
4
+
5
+ export default [
6
+ {languageOptions: { globals: {...globals.browser, ...globals.node} }},
7
+ pluginJs.configs.recommended,
8
+ ];
package/moon.yml CHANGED
@@ -1,5 +1,10 @@
1
1
  language: javascript
2
2
  tasks:
3
+ build:
4
+ command:
5
+ - node
6
+ - build.mjs
7
+ platform: node
3
8
  pub:
4
9
  command:
5
10
  - npm
package/package.json CHANGED
@@ -1,14 +1,25 @@
1
1
  {
2
- "name": "@jrc03c/pause",
3
- "version": "0.0.4",
2
+ "author": "jrc03c",
4
3
  "description": "a simple async pause function",
5
- "main": "index.js",
6
- "license": "ISC",
4
+ "devDependencies": {
5
+ "@eslint/js": "^9.12.0",
6
+ "esbuild": "^0.24.0",
7
+ "eslint": "^9.12.0",
8
+ "globals": "^15.11.0",
9
+ "prettier": "^3.3.3"
10
+ },
11
+ "exports": {
12
+ "import": "./src/index.mjs",
13
+ "require": "./dist/pause.require.cjs"
14
+ },
7
15
  "keywords": [
8
- "pause",
9
16
  "js",
17
+ "pause",
10
18
  "settimeout"
11
19
  ],
12
- "author": "jrc03c",
13
- "type": "module"
20
+ "license": "ISC",
21
+ "main": "src/index.mjs",
22
+ "name": "@jrc03c/pause",
23
+ "type": "module",
24
+ "version": "0.0.5"
14
25
  }
package/src/index.mjs ADDED
@@ -0,0 +1,29 @@
1
+ function pauseAsync(ms) {
2
+ return new Promise((resolve, reject) => {
3
+ try {
4
+ const start = new Date()
5
+ return setTimeout(() => resolve(new Date() - start), ms)
6
+ } catch (e) {
7
+ return reject(e)
8
+ }
9
+ })
10
+ }
11
+
12
+ function pauseSync(ms) {
13
+ const start = new Date()
14
+ let now = new Date()
15
+
16
+ while (now - start < ms) {
17
+ now = new Date()
18
+ }
19
+
20
+ return new Date() - start
21
+ }
22
+
23
+ if (typeof window !== "undefined") {
24
+ window.pause = pauseAsync
25
+ window.pauseAsync = pauseAsync
26
+ window.pauseSync = pauseSync
27
+ }
28
+
29
+ export { pauseAsync as pause, pauseAsync, pauseSync }
@@ -0,0 +1,63 @@
1
+ import { pauseAsync, pauseSync } from "./index.mjs"
2
+
3
+ let syncTestsAreRunning = true
4
+
5
+ !(async () => {
6
+ while (syncTestsAreRunning) {
7
+ await pauseAsync(100)
8
+ }
9
+
10
+ const start = new Date()
11
+
12
+ for (let i = 0; i < 100; i++) {
13
+ const t = Math.floor(Math.random() * 100)
14
+ const elapsed = await pauseAsync(t)
15
+
16
+ if (typeof elapsed !== "number" || isNaN(elapsed)) {
17
+ throw new Error(
18
+ `The function was supposed to return a number representing the number of milliseconds that elapsed since the function was called, but instead it returned a ${typeof elapsed} value! (${elapsed})`,
19
+ )
20
+ }
21
+
22
+ if (elapsed < t) {
23
+ throw new Error(
24
+ `The function was supposed to wait for ${t}ms, but instead it only waited for ${elapsed}ms!`,
25
+ )
26
+ }
27
+ }
28
+
29
+ const seconds = (new Date() - start) / 1000
30
+
31
+ console.log(
32
+ `🎉 All asynchronous tests passed! (${seconds.toFixed(2)} seconds)`,
33
+ )
34
+ })()
35
+
36
+ !(() => {
37
+ const start = new Date()
38
+
39
+ for (let i = 0; i < 100; i++) {
40
+ const t = Math.floor(Math.random() * 100)
41
+ const elapsed = pauseSync(t)
42
+
43
+ if (typeof elapsed !== "number" || isNaN(elapsed)) {
44
+ throw new Error(
45
+ `The function was supposed to return a number representing the number of milliseconds that elapsed since the function was called, but instead it returned a ${typeof elapsed} value! (${elapsed})`,
46
+ )
47
+ }
48
+
49
+ if (elapsed < t) {
50
+ throw new Error(
51
+ `The function was supposed to wait for ${t}ms, but instead it only waited for ${elapsed}ms!`,
52
+ )
53
+ }
54
+ }
55
+
56
+ const seconds = (new Date() - start) / 1000
57
+
58
+ console.log(
59
+ `🎉 All synchronous tests passed! (${seconds.toFixed(2)} seconds)`,
60
+ )
61
+
62
+ syncTestsAreRunning = false
63
+ })()
package/index.js DELETED
@@ -1,15 +0,0 @@
1
- function pause(ms) {
2
- return new Promise((resolve, reject) => {
3
- try {
4
- return setTimeout(() => resolve(), ms)
5
- } catch (e) {
6
- return reject(e)
7
- }
8
- })
9
- }
10
-
11
- if (typeof window !== "undefined") {
12
- window.pause = pause
13
- }
14
-
15
- export default pause