@wizhut_tech/wizjs 0.0.3 → 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/__tests__/lang_singleton.test.js +3 -3
- package/docs/lang_checks.md +3 -0
- package/docs/lang_flow.md +3 -0
- package/docs/lang_functools.md +3 -0
- package/docs/lang_singleton.md +8 -0
- package/docs/math_numbers.md +3 -0
- package/index.js +3 -1
- package/package.json +1 -1
- package/readme.md +28 -2
- package/src/lang/functools.js +10 -0
- package/src/lang/singleton.js +2 -2
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const t = require('tap');
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
const { getInstance,
|
|
4
|
+
const { getInstance, singleton } = require('../src/lang/singleton.js');
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
t.test('singleton/null-test', async (t) => {
|
|
@@ -12,11 +12,11 @@ t.test('singleton/null-test', async (t) => {
|
|
|
12
12
|
|
|
13
13
|
t.test('singleton/simple-tests', async (t) => {
|
|
14
14
|
// numbers
|
|
15
|
-
|
|
15
|
+
singleton('number-wrapper', async () => 1);
|
|
16
16
|
t.equal(await getInstance('number-wrapper'), 1)
|
|
17
17
|
|
|
18
18
|
// null
|
|
19
|
-
|
|
19
|
+
singleton('null-wrapper', async () => null);
|
|
20
20
|
t.equal(await getInstance('null-wrapper'), null);
|
|
21
21
|
|
|
22
22
|
t.end();
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# Lang / Singleton
|
|
2
|
+
|
|
3
|
+
In-memory caching of an async function's result, ensuring that it will be called only once. Mimics similar python decorators. It can easily, be used to store class instance variables.
|
|
4
|
+
|
|
5
|
+
### Module API
|
|
6
|
+
|
|
7
|
+
* **singleton(name, fn)**: register an `async` function (`fn`) by name.
|
|
8
|
+
* **getInstance(name)**: returns the instance for a specific `name`. Note that the `fn` is executed only when this method is called.
|
package/index.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
const singleton = require('./src/lang/singleton.js');
|
|
2
2
|
const checks = require('./src/lang/checks.js');
|
|
3
3
|
const numbers = require('./src/math/numbers.js');
|
|
4
|
+
const functools = require('./src/lang/functools.js');
|
|
4
5
|
|
|
5
6
|
|
|
6
7
|
module.exports = {
|
|
7
8
|
lang: {
|
|
8
9
|
singleton: singleton,
|
|
9
|
-
checks: checks
|
|
10
|
+
checks: checks,
|
|
11
|
+
functools: functools
|
|
10
12
|
},
|
|
11
13
|
math: {
|
|
12
14
|
numbers: numbers
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# Wizjs
|
|
2
2
|
|
|
3
|
-
A Javascript library (a few selected dependencies only) that contains utilities for enjoy-full every day programming.
|
|
3
|
+
A Javascript library (a few selected dependencies only) that contains utilities for enjoy-full every day programming. No, this library will not become another `lodash` :).
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Use by importing:
|
|
6
|
+
|
|
7
|
+
`const wizjs = require('@wizhut_tech/wizjs')`. Returns an object structured like:
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
{
|
|
11
|
+
lang: {
|
|
12
|
+
checks: [functions],
|
|
13
|
+
singleton: [functions],
|
|
14
|
+
functools: [functions]
|
|
15
|
+
},
|
|
16
|
+
math: {
|
|
17
|
+
numbers: [functions]
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### Language
|
|
23
|
+
|
|
24
|
+
* **Check** utility functions ... [[docs](docs/lang_checks.md)]
|
|
25
|
+
* Control-**Flow** utilities ... [[docs](docs/lang_flow.md)] -- *still in development* --
|
|
26
|
+
* **functools** ... [[docs](docs/lang_functools.md)]
|
|
27
|
+
* **Singleton** hack ... [[docs](docs/lang_singleton.md)]
|
|
28
|
+
|
|
29
|
+
### Math
|
|
30
|
+
|
|
31
|
+
* Utilities around **numbers** ... [[docs](docs/math_numbers.md)]
|
package/src/lang/singleton.js
CHANGED
|
@@ -6,7 +6,7 @@ const wrappers = {};
|
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
function
|
|
9
|
+
function singleton(name, fn) {
|
|
10
10
|
wrappers[name] = fn;
|
|
11
11
|
}
|
|
12
12
|
|
|
@@ -30,6 +30,6 @@ async function getInstance(name){
|
|
|
30
30
|
|
|
31
31
|
|
|
32
32
|
module.exports = {
|
|
33
|
-
|
|
33
|
+
singleton,
|
|
34
34
|
getInstance
|
|
35
35
|
}
|