@wizhut_tech/wizjs 0.0.8 → 0.1.0

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.
@@ -0,0 +1,21 @@
1
+ const t = require('tap');
2
+
3
+ const { cycle, fn_cycle } = require('../src/lang/itertools.js');
4
+
5
+
6
+ t.test('itertools/cycle', (t) => {
7
+ // simple integer
8
+ const genOne = cycle(0, 1);
9
+ t.equal(genOne.next().value, 0);
10
+ t.equal(genOne.next().value, 1);
11
+ t.equal(genOne.next().value, 2);
12
+
13
+ // simple floating-point
14
+ const genTwo = cycle(0, 0.5);
15
+ t.equal(genTwo.next().value, 0);
16
+ t.equal(genTwo.next().value, 0.5);
17
+ t.equal(genTwo.next().value, 1);
18
+ t.equal(genTwo.next().value, 1.5);
19
+
20
+ t.end();
21
+ });
@@ -0,0 +1,3 @@
1
+ # Lang / files
2
+
3
+ * **loadFully(filename)**: loads a *file*, fully and returns it as text. If an error occurs return null
package/docs/lang_flow.md CHANGED
@@ -1,3 +1,3 @@
1
1
  # Lang / flow
2
2
 
3
- **Still in development**
3
+ * **if_exception(fn, fn_noexc, fn_exc)**: executes *fn*. if it throws an exception then it calls *fn_exc*, else *fn_noexc*.
@@ -0,0 +1,4 @@
1
+ # Lang / itertools
2
+
3
+ * **cycle(start, step=1)**: A generator that counts from *start* with a specified *step*. Default stepping is *1*.
4
+ * **fn_cycle(start, step=1, fn)**: A generator that counts from *start* with a specified *step*. Default stepping is *1*. For each iteration the *fn* is called. To continue to the next iteration, the *fn* should return *true*. If not the iteration breaks,.
package/index.js CHANGED
@@ -3,9 +3,15 @@ const checks = require('./src/lang/checks.js');
3
3
  const numbers = require('./src/math/numbers.js');
4
4
  const functools = require('./src/lang/functools.js');
5
5
  const arrays = require('./src/lang/arrays.js');
6
+ const files = require('./src/io/files.js');
7
+
8
+ const { BadlyInitializedError } = require('./src/internal/exceptions.js');
6
9
 
7
10
 
8
11
  module.exports = {
12
+ io: {
13
+ files: files,
14
+ },
9
15
  lang: {
10
16
  arrays: arrays,
11
17
  singleton: singleton,
@@ -14,5 +20,8 @@ module.exports = {
14
20
  },
15
21
  math: {
16
22
  numbers: numbers
23
+ },
24
+ exceptions: {
25
+ BadlyInitializedError
17
26
  }
18
27
  }
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@wizhut_tech/wizjs",
3
- "version": "0.0.8",
3
+ "version": "0.1.0",
4
4
  "main": "index.js",
5
5
  "devDependencies": {
6
- "tap": "21.0.2"
6
+ "tap": "21.1.0"
7
7
  },
8
8
  "description": "Simple but useful hacks for everyday javascript programming",
9
9
  "scripts": {
package/readme.md CHANGED
@@ -8,9 +8,13 @@ Use by importing:
8
8
 
9
9
  ```
10
10
  {
11
+ io: {
12
+ files: [functions]
13
+ },
11
14
  lang: {
12
15
  arrays: [functions],
13
16
  checks: [functions],
17
+ flow: [functions],
14
18
  singleton: [functions],
15
19
  functools: [functions]
16
20
  },
@@ -20,12 +24,21 @@ Use by importing:
20
24
  }
21
25
  ```
22
26
 
27
+ You can also import individual functions like the following snippet:
28
+
29
+ `const { lang: { checks : { isNil } } } = require('@wizhut_tech/wizjs');`
30
+
31
+ ### I/O
32
+
33
+ * **Files** utility functions ... [[docs](docs/io_files.md)]
34
+
23
35
  ### Language
24
36
 
25
37
  * **Arrays** utility functions ... [[docs](docs/lang_arrays.md)]
26
38
  * **Check** utility functions ... [[docs](docs/lang_checks.md)]
27
- * Control-**Flow** utilities ... [[docs](docs/lang_flow.md)] -- *still in development* --
39
+ * Control-**Flow** utilities ... [[docs](docs/lang_flow.md)]
28
40
  * **functools** ... [[docs](docs/lang_functools.md)]
41
+ * **itertools** ... [[docs](docs/lang_itertools.md)]
29
42
  * **Singleton** hack ... [[docs](docs/lang_singleton.md)]
30
43
 
31
44
  ### Math
@@ -0,0 +1,12 @@
1
+
2
+
3
+ class BadlyInitializedError extends Error {
4
+ constructor(message, ...args) {
5
+ super(message);
6
+ }
7
+ }
8
+
9
+
10
+ module.exports = {
11
+ BadlyInitializedError
12
+ };
@@ -0,0 +1,15 @@
1
+ const fs = require('fs').promises;
2
+
3
+
4
+ async function loadFully(filename) {
5
+ try {
6
+ const data = await fs.readFile(filename, 'utf8');
7
+ } catch (err) {
8
+ return null;
9
+ }
10
+ }
11
+
12
+
13
+ module.exports = {
14
+ loadFully
15
+ };
package/src/lang/flow.js CHANGED
@@ -1,17 +1,15 @@
1
1
 
2
2
 
3
- class TransactionItem {
4
- constructor(task, fallback) {
5
- this.task = task;
6
- this.fallback = fallback;
7
- }
8
-
9
- async execute() {
10
-
3
+ function if_exception(fn, fn_noexc, fn_exc) {
4
+ try {
5
+ fn();
6
+ fn_noexc();
7
+ } catch (error) {
8
+ fn_exc(error);
11
9
  }
12
10
  }
13
11
 
14
12
 
15
13
  module.exports = {
16
-
14
+ if_exception
17
15
  };
@@ -0,0 +1,25 @@
1
+
2
+ const {toInteger} = require("../math/numbers");
3
+
4
+
5
+ function* cycle(start=0, step=1) {
6
+ let iCount = toInteger(start);
7
+ const iStep = toInteger(step);
8
+
9
+ while (true) {
10
+ yield iCount;
11
+ iCount += step;
12
+ }
13
+ }
14
+
15
+ function fn_cycle(start, fn, step=1) {
16
+ const cycleGenerator = cycle(start, step);
17
+
18
+ while (fn(cycleGenerator.next().value)) { }
19
+ }
20
+
21
+
22
+ module.exports = {
23
+ cycle,
24
+ fn_cycle
25
+ };