@sc-voice/tools 1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 SuttaCentral Voice
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # merkle-json
2
+ Computing the hash of JSON objects can be tricky because JSON.stringify()
3
+ does not have a guaranteed string representation of a Javascript object.
4
+ Specifically, the following are equivalent and valid outputs of JSON.stringify():
5
+
6
+ ```js
7
+ var json = "{size:{w:100,h:200}}";
8
+ var json = "{size:{h:100,w:200}}";
9
+ ```
10
+
11
+ MerkleJson guarantees a unique hash code for any Javascript object.
12
+ In addition, MerkleJson is efficient in that it only recalculates
13
+ object hashes if the object has no Merkle hash tag. If a Merkle hash
14
+ tag is present, its value is returned as the hash value of that object.
15
+
16
+ ### Serialize JSON object
17
+ Unlike JSON, MerkleJson serializes objects canonically.
18
+
19
+ ```js
20
+ var obj = {
21
+ d:4,
22
+ b:2,
23
+ a:1,
24
+ c:3,
25
+ };
26
+ console.log(JSON.stringify(obj)); // {"d":4,"b":2,"a":1,"c":3}
27
+ var mj = new MerkleJson();
28
+ console.log(mj.stringify(obj)); // {"a":1,"b":2,"c":3,"d":4}
29
+ ```
30
+
31
+ ### Compute hash of JSON object
32
+ ```js
33
+ var mj = new MerkleJson();
34
+ var hash = mj.hash({
35
+ size:{
36
+ w:100,
37
+ h:200
38
+ }
39
+ }); // e77b735125fec27a61c6f54b17fb6221
40
+
41
+ var hash = mj.hash({
42
+ size:{ // hash is independent of property order
43
+ h:200
44
+ w:100,
45
+ }
46
+ }); // e77b735125fec27a61c6f54b17fb6221
47
+ ```
48
+ ### Do not calculate hash if merkleHash is present
49
+ ```js
50
+ var mj = new MerkleJson();
51
+ var useMerkleHash = true;
52
+ var hash = mj.hash({
53
+ any1: thing1,
54
+ any2: thing2,
55
+ any3: thing3,
56
+ }, useMerkleHash); // 441e4f8dabdc6cb17dc9500cee73155b
57
+
58
+ var hash = mj.hash({
59
+ ... // anything
60
+ merkleHash: e77b735125fec27a61c6f54b17fb6221,
61
+ }, useMerkleHash); // e77b735125fec27a61c6f54b17fb6221
62
+
63
+ var useMerkleHash = false; // force hash calculation
64
+ var hash = mj.hash({
65
+ any1: thing1,
66
+ any2: thing2,
67
+ any3: thing3,
68
+ merkleHash: e77b735125fec27a61c6f54b17fb6221, // ignored
69
+ }, useMerkleHash); // 441e4f8dabdc6cb17dc9500cee73155b
70
+ ```
package/index.mjs ADDED
@@ -0,0 +1,31 @@
1
+
2
+ import { Fraction } from './src/math/fraction.mjs';
3
+
4
+ export const ScvMath = {
5
+ Fraction,
6
+ };
7
+
8
+ import {
9
+ Aligner, Alignment, AlignmentStatus
10
+ } from './src/text/aligner.mjs';
11
+ import { BilaraPath } from './src/text/bilara-path.mjs';
12
+ import { EbtDoc } from './src/text/ebt-doc.mjs';
13
+ import { LegacyDoc } from './src/text/legacy-doc.mjs';
14
+ import { MerkleJson } from './src/text/merkle-json.mjs';
15
+ import { SuttaCentralId } from './src/text/sutta-central-id.mjs';
16
+ import { Unicode } from './src/text/unicode.mjs';
17
+ import { WordSpace } from './src/text/word-space.mjs';
18
+
19
+ export const Text = {
20
+ Aligner,
21
+ Alignment,
22
+ AlignmentStatus,
23
+ BilaraPath,
24
+ EbtDoc,
25
+ LegacyDoc,
26
+ MerkleJson,
27
+ SuttaCentralId,
28
+ Unicode,
29
+ WordSpace,
30
+ };
31
+
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@sc-voice/tools",
3
+ "version": "1.1.0",
4
+ "description": "Utilities for SC-Voice",
5
+ "main": "index.mjs",
6
+ "files": [
7
+ "LICENSE",
8
+ "index.mjs",
9
+ "package.json",
10
+ "README.md",
11
+ "src"
12
+ ],
13
+ "scripts": {
14
+ "test": "mocha --inline-diffs --recursive",
15
+ "test:test": "mocha --config test/mocha-config.json --recursive"
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/sc-voice/tools.git"
20
+ },
21
+ "keywords": [
22
+ "SC-Voice tools",
23
+ "merkle-json",
24
+ "Javascript"
25
+ ],
26
+ "author": "Karl Lew",
27
+ "license": "MIT",
28
+ "bugs": {
29
+ "url": "https://github.com/sc-voice/tools/issues"
30
+ },
31
+ "homepage": "https://github.com/sc-voice/tools#readme",
32
+ "devDependencies": {
33
+ "@biomejs/biome": "1.9.4",
34
+ "eslint": "^9.17.0",
35
+ "mocha": "^11.0.1",
36
+ "should": "^13.2.3"
37
+ }
38
+ }
@@ -0,0 +1,9 @@
1
+
2
+ export const DBG = {
3
+ APPLY_WORD_MAP: 0,
4
+ ALIGN_ALL: 0,
5
+ ALIGN_LINE: 0,
6
+ ML_DOC_VECTORS: 0, // 'mn8:3.4',
7
+ MN8_MOHAN: 0,
8
+ };
9
+
@@ -0,0 +1,94 @@
1
+ export class Fraction {
2
+ constructor(...args) {
3
+ //constructor(numerator, denominator = 1, units = undefined) {
4
+ const msg = 'Fraction.ctor:';
5
+ if (args[0] instanceof Fraction) {
6
+ let { numerator: n, denominator: d, units: u } = args[0];
7
+ args = [n, d, u];
8
+ }
9
+ let [numerator, denominator = 1, units = undefined] = args;
10
+
11
+ Object.assign(this, {
12
+ numerator,
13
+ denominator,
14
+ units,
15
+ });
16
+ }
17
+
18
+ static gcd(a, b) {
19
+ if (b === 0) {
20
+ return a;
21
+ }
22
+ return Fraction.gcd(b, a % b);
23
+ }
24
+
25
+ get remainder() {
26
+ let { n, d } = this;
27
+
28
+ return n % d;
29
+ }
30
+
31
+ get difference() {
32
+ let { n, d } = this;
33
+ return n - d;
34
+ }
35
+
36
+ get percent() {
37
+ return (this.value * 100).toFixed(0) + '%';
38
+ }
39
+
40
+ get n() {
41
+ return this.numerator;
42
+ }
43
+
44
+ get d() {
45
+ return this.denominator;
46
+ }
47
+
48
+ get value() {
49
+ let { numerator, denominator } = this;
50
+ return numerator / denominator;
51
+ }
52
+
53
+ increment(delta = 1) {
54
+ this.numerator += Math.round(delta);
55
+ return this;
56
+ }
57
+
58
+ reduce() {
59
+ let { numerator: n, denominator: d, units } = this;
60
+ if (Number.isInteger(n) && Number.isInteger(d)) {
61
+ let g = Fraction.gcd(n, d);
62
+ if (g) {
63
+ this.numerator /= g;
64
+ this.denominator /= g;
65
+ }
66
+ }
67
+ return this;
68
+ }
69
+
70
+ toString() {
71
+ let { units, numerator: n, denominator: d, value } = this;
72
+ if (n == null || d == null) {
73
+ return `${n}/${d}`;
74
+ }
75
+ if (d < 0) {
76
+ d = -d;
77
+ n = -n;
78
+ }
79
+ let s = d === 1 ? `${n}` : `${n}/${d}`;
80
+
81
+ return units ? `${s} ${units}` : s;
82
+ }
83
+
84
+ add(f) {
85
+ const msg = 'Fraction.add:';
86
+ let { numerator: n1, denominator: d1, units: u1 } = this;
87
+ let { numerator: n2, denominator: d2, units: u2 } = f;
88
+ if (this.units !== f.units) {
89
+ throw new Error(`${msg} units? ${u1}? ${u2}?`);
90
+ }
91
+
92
+ return new Fraction(n1 * d2 + n2 * d1, d1 * d2).reduce();
93
+ }
94
+ }