@jrc03c/pause 0.0.4 → 0.0.6

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,26 @@
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
+ `${baseCommand} --format=esm --outfile=dist/pause.import.mjs`,
14
+ ]
15
+
16
+ commands.forEach(command => {
17
+ execSync(command, { encoding: "utf8" })
18
+ })
19
+
20
+ console.log("\nDone! 🎉\n")
21
+ } catch (e) {
22
+ console.error(e)
23
+ }
24
+ }
25
+
26
+ rebuild()
@@ -0,0 +1,29 @@
1
+ // src/index.mjs
2
+ function pauseAsync(ms) {
3
+ return new Promise((resolve, reject) => {
4
+ try {
5
+ const start = /* @__PURE__ */ new Date();
6
+ return setTimeout(() => resolve(/* @__PURE__ */ new Date() - start), ms);
7
+ } catch (e) {
8
+ return reject(e);
9
+ }
10
+ });
11
+ }
12
+ function pauseSync(ms) {
13
+ const start = /* @__PURE__ */ new Date();
14
+ let now = /* @__PURE__ */ new Date();
15
+ while (now - start < ms) {
16
+ now = /* @__PURE__ */ new Date();
17
+ }
18
+ return /* @__PURE__ */ new Date() - start;
19
+ }
20
+ if (typeof window !== "undefined") {
21
+ window.pause = pauseAsync;
22
+ window.pauseAsync = pauseAsync;
23
+ window.pauseSync = pauseSync;
24
+ }
25
+ export {
26
+ pauseAsync as pause,
27
+ pauseAsync,
28
+ pauseSync
29
+ };
@@ -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
@@ -17,3 +22,8 @@ tasks:
17
22
  - --force
18
23
  local: true
19
24
  platform: node
25
+ test:
26
+ command:
27
+ - node
28
+ - src/index.test.mjs
29
+ platform: node
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": "./dist/pause.import.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": "./dist/pause.require.cjs",
22
+ "name": "@jrc03c/pause",
23
+ "type": "module",
24
+ "version": "0.0.6"
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,65 @@
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
+ // there should be less than 10ms difference between the given time and the
23
+ // actual elapsed time
24
+ if (Math.abs(elapsed - t) > 10) {
25
+ throw new Error(
26
+ `The function was supposed to wait for ${t}ms, but instead it waited for ${elapsed}ms!`,
27
+ )
28
+ }
29
+ }
30
+
31
+ const seconds = (new Date() - start) / 1000
32
+
33
+ console.log(
34
+ `🎉 All asynchronous tests passed! (${seconds.toFixed(2)} seconds)`,
35
+ )
36
+ })()
37
+
38
+ !(() => {
39
+ const start = new Date()
40
+
41
+ for (let i = 0; i < 100; i++) {
42
+ const t = Math.floor(Math.random() * 100)
43
+ const elapsed = pauseSync(t)
44
+
45
+ if (typeof elapsed !== "number" || isNaN(elapsed)) {
46
+ throw new Error(
47
+ `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})`,
48
+ )
49
+ }
50
+
51
+ if (elapsed < t) {
52
+ throw new Error(
53
+ `The function was supposed to wait for ${t}ms, but instead it only waited for ${elapsed}ms!`,
54
+ )
55
+ }
56
+ }
57
+
58
+ const seconds = (new Date() - start) / 1000
59
+
60
+ console.log(
61
+ `🎉 All synchronous tests passed! (${seconds.toFixed(2)} seconds)`,
62
+ )
63
+
64
+ syncTestsAreRunning = false
65
+ })()
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