get-symbols 0.0.1

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/.editorconfig ADDED
@@ -0,0 +1,8 @@
1
+ root = true
2
+
3
+ [*]
4
+ indent_style = tab
5
+ charset = utf-8
6
+ trim_trailing_whitespace = true
7
+ insert_final_newline = true
8
+ end_of_line = lf
@@ -0,0 +1,17 @@
1
+ {
2
+ "plugins": [
3
+ "prettier-plugin-brace-style",
4
+ "prettier-plugin-space-before-function-paren",
5
+ "prettier-plugin-merge"
6
+ ],
7
+ "braceStyle": "stroustrup",
8
+ "arrowParens": "avoid",
9
+ "bracketSpacing": true,
10
+ "endOfLine": "auto",
11
+ "semi": true,
12
+ "singleQuote": false,
13
+ "tabWidth": 4,
14
+ "useTabs": true,
15
+ "trailingComma": "all",
16
+ "printWidth": 100
17
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "editor.formatOnSave": true,
3
+ "editor.defaultFormatter": "esbenp.prettier-vscode",
4
+ "prettier.enable": true,
5
+ "debug.enableStatusBarColor": false
6
+ }
package/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # Get symbols
2
+
3
+ Hassle-free JS symbols. Like, lots of them.
4
+
5
+ get-symbols lets you easily create new symbols through object destructuring:
6
+
7
+ ```js
8
+ import symbols from "get-symbols";
9
+
10
+ let { foo, bar, baz } = symbols.new;
11
+ // console.log(foo, bar, baz); // Symbol(foo) Symbol(bar) Symbol(baz)
12
+ ```
13
+
14
+ `symbols.new` will always create new symbols, even for the same name:
15
+
16
+ ```js
17
+ foo === symbols.new.foo; // false
18
+ ```
19
+
20
+ If you'd rather reuse symbols with the same name, you can use `symbols.known`:
21
+
22
+ ```js
23
+ import symbols from "symbols";
24
+
25
+ let { foo, bar, baz } = symbols.known;
26
+ // foo, bar, baz are new symbols
27
+ // with descriptions "foo", "bar", "baz"
28
+
29
+ foo === symbols.known.foo; // true
30
+ ```
31
+
32
+ You can import `KNOWN_SYMBOLS` to get the symbols registry:
33
+
34
+ ```js
35
+ import { KNOWN_SYMBOLS } from "symbols";
36
+
37
+ console.log(KNOWN_SYMBOLS); // { foo: Symbol(foo), bar: Symbol(bar), baz: Symbol(baz) }
38
+ ```
39
+
40
+ ## Custom registries
41
+
42
+ You can create your own symbols registry by calling `symbols.registry()` that behaves like `symbols.known`:
43
+
44
+ ```js
45
+ import symbols from "symbols";
46
+
47
+ let registry = symbols.registry();
48
+
49
+ let { foo, bar, baz } = registry;
50
+
51
+ console.log(foo, bar, baz); // Symbol(foo) Symbol(bar) Symbol(baz)
52
+ ```
package/index.js ADDED
@@ -0,0 +1,43 @@
1
+ export const KNOWN_SYMBOLS = {};
2
+
3
+ export const newSymbols = new Proxy(
4
+ {},
5
+ {
6
+ get (target, prop) {
7
+ if (typeof prop === "string") {
8
+ return Symbol(prop);
9
+ }
10
+
11
+ return target[prop];
12
+ },
13
+ },
14
+ );
15
+
16
+ export function registry (knownSymbols = {}) {
17
+ return new Proxy(
18
+ {},
19
+ {
20
+ get (target, prop) {
21
+ if (typeof prop === "string") {
22
+ if (knownSymbols[prop]) {
23
+ return knownSymbols[prop];
24
+ }
25
+
26
+ let ret = Symbol(prop);
27
+ knownSymbols[prop] = ret;
28
+ return ret;
29
+ }
30
+
31
+ return target[prop];
32
+ },
33
+ },
34
+ );
35
+ }
36
+
37
+ export const known = registry(KNOWN_SYMBOLS);
38
+
39
+ export default {
40
+ new: newSymbols,
41
+ known,
42
+ registry,
43
+ };
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "get-symbols",
3
+ "version": "0.0.1",
4
+ "description": "Description",
5
+ "keywords": [],
6
+ "homepage": "https://github.com/leaverou/get-symbols/#readme",
7
+ "bugs": {
8
+ "url": "https://github.com/leaverou/get-symbols/issues"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/leaverou/get-symbols.git"
13
+ },
14
+ "license": "MIT",
15
+ "author": "Lea Verou",
16
+ "type": "module",
17
+ "main": "src/index.js",
18
+ "exports": {
19
+ ".": "./index.js"
20
+ },
21
+ "scripts": {
22
+ "release": "npm login && release-it",
23
+ "test": "npx htest test/index.js"
24
+ },
25
+ "devDependencies": {
26
+ "htest.dev": "^0.0.23",
27
+ "prettier": "~3.5.3",
28
+ "prettier-plugin-brace-style": "latest",
29
+ "prettier-plugin-merge": "latest",
30
+ "prettier-plugin-space-before-function-paren": "latest",
31
+ "release-it": "latest"
32
+ }
33
+ }
package/test/index.js ADDED
@@ -0,0 +1,102 @@
1
+ import { newSymbols, known, registry, KNOWN_SYMBOLS } from "../index.js";
2
+
3
+ export default {
4
+ name: "get-symbols",
5
+ tests: [
6
+ {
7
+ name: "newSymbols",
8
+ tests: [
9
+ {
10
+ name: "Creates a Symbol with correct description",
11
+ run: arg => newSymbols[arg].description,
12
+ tests: [
13
+ { arg: "foo", expect: "foo" },
14
+ { arg: "myName", expect: "myName" },
15
+ ],
16
+ },
17
+ {
18
+ name: "Always creates a new Symbol",
19
+ run: () => newSymbols.same === newSymbols.same,
20
+ expect: false,
21
+ },
22
+ ],
23
+ },
24
+ {
25
+ name: "known",
26
+ tests: [
27
+ {
28
+ name: "Creates a Symbol with correct description",
29
+ run: arg => known[arg].description,
30
+ tests: [
31
+ { arg: "testKnown", expect: "testKnown" },
32
+ { arg: "testDesc", expect: "testDesc" },
33
+ ],
34
+ },
35
+ {
36
+ name: "Returns the same Symbol on repeated access",
37
+ run () {
38
+ let first = known.memoTest;
39
+ let second = known.memoTest;
40
+ return first === second;
41
+ },
42
+ expect: true,
43
+ },
44
+ {
45
+ name: "Stores symbols in KNOWN_SYMBOLS",
46
+ run () {
47
+ let sym = known.storedTest;
48
+ return KNOWN_SYMBOLS.storedTest === sym;
49
+ },
50
+ expect: true,
51
+ },
52
+ ],
53
+ },
54
+ {
55
+ name: "registry()",
56
+ tests: [
57
+ {
58
+ name: "Creates a registry that produces Symbols",
59
+ run: () => typeof registry().foo,
60
+ expect: "symbol",
61
+ },
62
+ {
63
+ name: "Memoizes within the registry",
64
+ run () {
65
+ let r = registry();
66
+ return r.x === r.x;
67
+ },
68
+ expect: true,
69
+ },
70
+ {
71
+ name: "Stores symbols in the provided object",
72
+ run () {
73
+ let store = {};
74
+ let r = registry(store);
75
+ let sym = r.myKey;
76
+ return store.myKey === sym;
77
+ },
78
+ expect: true,
79
+ },
80
+ {
81
+ name: "Registries are isolated from each other",
82
+ run () {
83
+ let r1 = registry();
84
+ let r2 = registry();
85
+ return r1.foo === r2.foo;
86
+ },
87
+ expect: false,
88
+ },
89
+ {
90
+ name: "Uses pre-existing symbols from the provided object",
91
+ run () {
92
+ let existing = Symbol("pre");
93
+ let store = { pre: existing };
94
+ let r = registry(store);
95
+ return r.pre === existing;
96
+ },
97
+ expect: true,
98
+ },
99
+ ],
100
+ },
101
+ ],
102
+ };