@thi.ng/dynvar 0.3.47 → 0.3.48

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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2023-12-09T19:12:03Z
3
+ - **Last updated**: 2023-12-11T10:07:09Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
package/README.md CHANGED
@@ -53,7 +53,7 @@ For Node.js REPL:
53
53
  const dynvar = await import("@thi.ng/dynvar");
54
54
  ```
55
55
 
56
- Package sizes (brotli'd, pre-treeshake): ESM: 266 bytes
56
+ Package sizes (brotli'd, pre-treeshake): ESM: 265 bytes
57
57
 
58
58
  ## Dependencies
59
59
 
package/index.js CHANGED
@@ -1,96 +1,83 @@
1
1
  import { assert } from "@thi.ng/errors/assert";
2
- /**
3
- * Hidden storage for bound value stacks
4
- */
5
- const __private = new WeakMap();
6
- /**
7
- * Returns new {@link DynVar} with given value `x` as its root binding.
8
- *
9
- * @param x - initial value
10
- */
11
- export const dynvar = (x) => new DynVar(x);
12
- /**
13
- * Simple dynamic scope container & implementation.
14
- *
15
- * References:
16
- *
17
- * - https://en.wikipedia.org/wiki/Scope_(computer_science)#Dynamic_scoping
18
- *
19
- */
20
- export class DynVar {
21
- constructor(val) {
22
- __private.set(this, [val]);
23
- }
24
- /**
25
- * Returns current bound value, i.e. that of the currently active
26
- * dynamic scope.
27
- */
28
- deref() {
29
- const v = __private.get(this);
30
- return v[v.length - 1];
31
- }
32
- /**
33
- * Same as {@link DynVar.deref}, but also for `DynVar` to satisfy
34
- * `Object.valueOf()` contract.
35
- *
36
- */
37
- valueOf() {
38
- return this.deref();
39
- }
40
- /**
41
- * Returns new `DynVar` with this var's current value as its root
42
- * binding.
43
- */
44
- copy() {
45
- return new DynVar(this.deref());
46
- }
47
- /**
48
- * Starts new dynamic scope in which given `val` will be bound to
49
- * this variable. In most cases, calls to `bind()` should always be
50
- * eventually followed by calls to {@link DynVar.unbind} to restore
51
- * this var's previously scoped value.
52
- *
53
- * @param val -
54
- */
55
- bind(val) {
56
- __private.get(this).push(val);
57
- return true;
58
- }
59
- /**
60
- * Attempts to end the current scope by restoring this var's bound
61
- * value to that of parent scope. An error is thrown if attempting
62
- * to remove the root binding.
63
- */
64
- unbind() {
65
- const v = __private.get(this);
66
- assert(v.length > 1, `can't unbind root value`);
67
- v.pop();
68
- return true;
69
- }
70
- /**
71
- * Replaces current scope's value with new `val`.
72
- *
73
- * @param val -
74
- */
75
- set(val) {
76
- const v = __private.get(this);
77
- v[v.length - 1] = val;
78
- }
79
- /**
80
- * Executes given `body` function in a new scope which has given
81
- * `val` bound to this variable. The new scope is automatically
82
- * removed when the function returns or an error occurred.
83
- *
84
- * @param val -
85
- * @param body -
86
- */
87
- withBinding(val, body) {
88
- this.bind(val);
89
- try {
90
- return body();
91
- }
92
- finally {
93
- this.unbind();
94
- }
2
+ const __private = /* @__PURE__ */ new WeakMap();
3
+ const dynvar = (x) => new DynVar(x);
4
+ class DynVar {
5
+ constructor(val) {
6
+ __private.set(this, [val]);
7
+ }
8
+ /**
9
+ * Returns current bound value, i.e. that of the currently active
10
+ * dynamic scope.
11
+ */
12
+ deref() {
13
+ const v = __private.get(this);
14
+ return v[v.length - 1];
15
+ }
16
+ /**
17
+ * Same as {@link DynVar.deref}, but also for `DynVar` to satisfy
18
+ * `Object.valueOf()` contract.
19
+ *
20
+ */
21
+ valueOf() {
22
+ return this.deref();
23
+ }
24
+ /**
25
+ * Returns new `DynVar` with this var's current value as its root
26
+ * binding.
27
+ */
28
+ copy() {
29
+ return new DynVar(this.deref());
30
+ }
31
+ /**
32
+ * Starts new dynamic scope in which given `val` will be bound to
33
+ * this variable. In most cases, calls to `bind()` should always be
34
+ * eventually followed by calls to {@link DynVar.unbind} to restore
35
+ * this var's previously scoped value.
36
+ *
37
+ * @param val -
38
+ */
39
+ bind(val) {
40
+ __private.get(this).push(val);
41
+ return true;
42
+ }
43
+ /**
44
+ * Attempts to end the current scope by restoring this var's bound
45
+ * value to that of parent scope. An error is thrown if attempting
46
+ * to remove the root binding.
47
+ */
48
+ unbind() {
49
+ const v = __private.get(this);
50
+ assert(v.length > 1, `can't unbind root value`);
51
+ v.pop();
52
+ return true;
53
+ }
54
+ /**
55
+ * Replaces current scope's value with new `val`.
56
+ *
57
+ * @param val -
58
+ */
59
+ set(val) {
60
+ const v = __private.get(this);
61
+ v[v.length - 1] = val;
62
+ }
63
+ /**
64
+ * Executes given `body` function in a new scope which has given
65
+ * `val` bound to this variable. The new scope is automatically
66
+ * removed when the function returns or an error occurred.
67
+ *
68
+ * @param val -
69
+ * @param body -
70
+ */
71
+ withBinding(val, body) {
72
+ this.bind(val);
73
+ try {
74
+ return body();
75
+ } finally {
76
+ this.unbind();
95
77
  }
78
+ }
96
79
  }
80
+ export {
81
+ DynVar,
82
+ dynvar
83
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/dynvar",
3
- "version": "0.3.47",
3
+ "version": "0.3.48",
4
4
  "description": "Dynamically scoped variable bindings",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -24,7 +24,9 @@
24
24
  "author": "Karsten Schmidt (https://thi.ng)",
25
25
  "license": "Apache-2.0",
26
26
  "scripts": {
27
- "build": "yarn clean && tsc --declaration",
27
+ "build": "yarn build:esbuild && yarn build:decl",
28
+ "build:decl": "tsc --declaration --emitDeclarationOnly",
29
+ "build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
28
30
  "clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc",
29
31
  "doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
30
32
  "doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
@@ -33,11 +35,12 @@
33
35
  "test": "bun test"
34
36
  },
35
37
  "dependencies": {
36
- "@thi.ng/api": "^8.9.11",
37
- "@thi.ng/errors": "^2.4.5"
38
+ "@thi.ng/api": "^8.9.12",
39
+ "@thi.ng/errors": "^2.4.6"
38
40
  },
39
41
  "devDependencies": {
40
42
  "@microsoft/api-extractor": "^7.38.3",
43
+ "esbuild": "^0.19.8",
41
44
  "rimraf": "^5.0.5",
42
45
  "tools": "^0.0.1",
43
46
  "typedoc": "^0.25.4",
@@ -75,5 +78,5 @@
75
78
  "year": 2016,
76
79
  "status": "alpha"
77
80
  },
78
- "gitHead": "25f2ac8ff795a432a930119661b364d4d93b59a0\n"
81
+ "gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n"
79
82
  }