@bitblit/ratchet-common 4.0.115-alpha → 4.0.119-alpha

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.
Files changed (114) hide show
  1. package/lib/index.d.ts +73 -1
  2. package/lib/index.mjs +4 -0
  3. package/lib/index.mjs.map +1 -0
  4. package/lib/lang/base64-ratchet.d.ts +1 -1
  5. package/lib/stream/buffer-writable.d.ts +2 -2
  6. package/lib/stream/multi-stream.d.ts +1 -1
  7. package/lib/stream/stream-ratchet.d.ts +2 -2
  8. package/lib/stream/string-writable.d.ts +1 -1
  9. package/package.json +9 -5
  10. package/lib/2d/line-2d.js +0 -1
  11. package/lib/2d/plane-2d.js +0 -1
  12. package/lib/2d/point-2d.js +0 -1
  13. package/lib/2d/poly-line-2d.js +0 -1
  14. package/lib/2d/ratchet-2d.js +0 -245
  15. package/lib/2d/ratchet-2d.spec.js +0 -117
  16. package/lib/build/build-information.js +0 -1
  17. package/lib/build/ratchet-common-info.js +0 -14
  18. package/lib/histogram/histogram-entry.js +0 -1
  19. package/lib/histogram/histogram.js +0 -48
  20. package/lib/histogram/histogram.spec.js +0 -19
  21. package/lib/index.js +0 -1
  22. package/lib/jwt/common-jwt-token.js +0 -1
  23. package/lib/jwt/expired-jwt-handling.js +0 -6
  24. package/lib/jwt/jwt-ratchet-like.js +0 -1
  25. package/lib/jwt/jwt-ratchet.js +0 -164
  26. package/lib/jwt/jwt-ratchet.spec.js +0 -49
  27. package/lib/jwt/jwt-token-base.js +0 -1
  28. package/lib/lang/array-ratchet.js +0 -83
  29. package/lib/lang/array-ratchet.spec.js +0 -68
  30. package/lib/lang/base64-ratchet.js +0 -45
  31. package/lib/lang/base64-ratchet.spec.js +0 -28
  32. package/lib/lang/boolean-ratchet.js +0 -45
  33. package/lib/lang/boolean-ratchet.spec.js +0 -81
  34. package/lib/lang/composite-last-success-provider.js +0 -26
  35. package/lib/lang/composite-last-success-provider.spec.js +0 -26
  36. package/lib/lang/date-ratchet.js +0 -29
  37. package/lib/lang/date-ratchet.spec.js +0 -21
  38. package/lib/lang/duration-ratchet.js +0 -42
  39. package/lib/lang/duration-ratchet.spec.js +0 -39
  40. package/lib/lang/enum-ratchet.js +0 -30
  41. package/lib/lang/enum-ratchet.spec.js +0 -31
  42. package/lib/lang/error-ratchet.js +0 -44
  43. package/lib/lang/error-ratchet.spec.js +0 -20
  44. package/lib/lang/esm-ratchet.js +0 -17
  45. package/lib/lang/expiring-object.js +0 -72
  46. package/lib/lang/expiring-object.spec.js +0 -51
  47. package/lib/lang/geolocation-ratchet.js +0 -263
  48. package/lib/lang/geolocation-ratchet.spec.js +0 -74
  49. package/lib/lang/global-ratchet.js +0 -43
  50. package/lib/lang/global-ratchet.spec.js +0 -14
  51. package/lib/lang/key-value.js +0 -4
  52. package/lib/lang/last-success-provider.js +0 -1
  53. package/lib/lang/map-ratchet.js +0 -164
  54. package/lib/lang/map-ratchet.spec.js +0 -90
  55. package/lib/lang/no.js +0 -4
  56. package/lib/lang/no.spec.js +0 -7
  57. package/lib/lang/number-ratchet.js +0 -155
  58. package/lib/lang/number-ratchet.spec.js +0 -118
  59. package/lib/lang/parsed-url.js +0 -1
  60. package/lib/lang/promise-ratchet.js +0 -138
  61. package/lib/lang/promise-ratchet.spec.js +0 -74
  62. package/lib/lang/require-ratchet.js +0 -56
  63. package/lib/lang/require-ratchet.spec.js +0 -80
  64. package/lib/lang/stop-watch.js +0 -114
  65. package/lib/lang/string-ratchet.js +0 -201
  66. package/lib/lang/string-ratchet.spec.js +0 -158
  67. package/lib/lang/time-zone-ratchet.js +0 -77
  68. package/lib/lang/time-zone-ratchet.spec.js +0 -40
  69. package/lib/lang/timeout-token.js +0 -17
  70. package/lib/lang/timeout-token.spec.js +0 -9
  71. package/lib/lang/transform-ratchet.js +0 -66
  72. package/lib/lang/transform-ratchet.spec.js +0 -118
  73. package/lib/logger/classic-single-line-log-message-formatter.js +0 -13
  74. package/lib/logger/log-message-builder.js +0 -48
  75. package/lib/logger/log-message-format-type.js +0 -6
  76. package/lib/logger/log-message-formatter.js +0 -1
  77. package/lib/logger/log-message-processor.js +0 -1
  78. package/lib/logger/log-message.js +0 -1
  79. package/lib/logger/log-snapshot.js +0 -1
  80. package/lib/logger/logger-instance.js +0 -208
  81. package/lib/logger/logger-level-name.js +0 -9
  82. package/lib/logger/logger-meta.js +0 -1
  83. package/lib/logger/logger-options.js +0 -1
  84. package/lib/logger/logger-output-function.js +0 -6
  85. package/lib/logger/logger-ring-buffer.js +0 -73
  86. package/lib/logger/logger-util.js +0 -44
  87. package/lib/logger/logger-util.spec.js +0 -9
  88. package/lib/logger/logger.js +0 -134
  89. package/lib/logger/logger.spec.js +0 -134
  90. package/lib/logger/none-log-message-formatter.js +0 -5
  91. package/lib/logger/structured-json-log-message-formatter.js +0 -19
  92. package/lib/network/browser-local-ip-provider.js +0 -21
  93. package/lib/network/browser-local-ip-provider.spec.js +0 -16
  94. package/lib/network/fixed-local-ip-provider.js +0 -9
  95. package/lib/network/local-ip-provider.js +0 -1
  96. package/lib/network/network-ratchet.js +0 -102
  97. package/lib/network/network-ratchet.spec.js +0 -13
  98. package/lib/stream/buffer-writable.js +0 -16
  99. package/lib/stream/multi-stream.js +0 -12
  100. package/lib/stream/stream-ratchet.js +0 -68
  101. package/lib/stream/stream-ratchet.spec.js +0 -13
  102. package/lib/stream/string-writable.js +0 -14
  103. package/lib/stream/string-writable.spec.js +0 -12
  104. package/lib/third-party/google/google-recaptcha-ratchet.js +0 -28
  105. package/lib/third-party/google/google-recaptcha-ratchet.spec.js +0 -20
  106. package/lib/third-party/twilio/twilio-ratchet.js +0 -73
  107. package/lib/transform/built-in-transforms.js +0 -191
  108. package/lib/transform/transform-rule.js +0 -1
  109. package/lib/tx/transaction-configuration.js +0 -1
  110. package/lib/tx/transaction-final-state.js +0 -6
  111. package/lib/tx/transaction-ratchet.js +0 -76
  112. package/lib/tx/transaction-ratchet.spec.js +0 -130
  113. package/lib/tx/transaction-result.js +0 -1
  114. package/lib/tx/transaction-step.js +0 -1
@@ -1,74 +0,0 @@
1
- import { GeolocationRatchet } from './geolocation-ratchet.js';
2
- import fs from 'fs';
3
- import { NumberRatchet } from './number-ratchet.js';
4
- import path from 'path';
5
- import { EsmRatchet } from './esm-ratchet.js';
6
- describe('#geolocationRatchet', function () {
7
- it('should canonicalize', function () {
8
- expect(GeolocationRatchet.combineBounds([])).toBeNull();
9
- expect(GeolocationRatchet.combineBounds(null)).toBeNull();
10
- });
11
- it('should canonicalize', function () {
12
- const bounds = {
13
- origin: {
14
- lat: 4,
15
- lng: 3,
16
- },
17
- extent: {
18
- lat: 2,
19
- lng: 5,
20
- },
21
- };
22
- const result = GeolocationRatchet.canonicalizeBounds(bounds);
23
- expect(result.origin.lat).toEqual(2);
24
- expect(result.origin.lng).toEqual(3);
25
- expect(result.extent.lat).toEqual(4);
26
- expect(result.extent.lng).toEqual(5);
27
- });
28
- it('should calc point in bounds', function () {
29
- const input = fs
30
- .readFileSync(path.join(EsmRatchet.fetchDirName(import.meta.url), '../../../../test-data/sample_geo_locations.csv'))
31
- .toString();
32
- const locations = input
33
- .split('\n')
34
- .map((line) => {
35
- const vals = line.split(',');
36
- if (!!vals && vals.length === 2) {
37
- return {
38
- lat: NumberRatchet.safeNumber(vals[0]),
39
- lng: NumberRatchet.safeNumber(vals[1]),
40
- };
41
- }
42
- else {
43
- return null;
44
- }
45
- })
46
- .filter((s) => !!s);
47
- const bounds = locations.map((l) => GeolocationRatchet.locationToBounds(l, 10));
48
- const mapping = GeolocationRatchet.buildRatchetLocationBoundsMap(bounds);
49
- const testPoint1 = locations[100];
50
- const testPoint2 = { lng: 5, lat: 5 };
51
- const testPoint3 = {
52
- lat: 40.7566,
53
- lng: -73.9887,
54
- };
55
- const testPoint4 = {
56
- lat: 33.74616,
57
- lng: -84.3708,
58
- };
59
- const testPoint5 = {
60
- lat: 37.790336,
61
- lng: -122.405399,
62
- };
63
- const pt1In = GeolocationRatchet.pointInAnyBound(testPoint1, bounds);
64
- const pt2In = GeolocationRatchet.pointInAnyBound(testPoint2, bounds);
65
- const pt3In = GeolocationRatchet.pointInAnyBound(testPoint3, bounds);
66
- const pt4In = GeolocationRatchet.pointInAnyBound(testPoint4, bounds);
67
- const pt5In = GeolocationRatchet.pointInRatchetLocationBoundsMap(testPoint5, mapping);
68
- expect(pt1In).toBeTruthy();
69
- expect(pt2In).toBeFalsy();
70
- expect(pt3In).toBeTruthy();
71
- expect(pt4In).toBeTruthy();
72
- expect(pt5In).toBeTruthy();
73
- });
74
- });
@@ -1,43 +0,0 @@
1
- import { ErrorRatchet } from './error-ratchet.js';
2
- import { RequireRatchet } from './require-ratchet.js';
3
- import { Logger } from '../logger/logger.js';
4
- export class GlobalRatchet {
5
- constructor() { }
6
- static fetchGlobalRecord(returnNullOnNone) {
7
- let rval = null;
8
- if (process?.env) {
9
- Logger.silly('Using process.env as global');
10
- rval = process.env;
11
- }
12
- else if (global?.process?.env) {
13
- Logger.silly('Using global.process.env as global');
14
- rval = global.process.env;
15
- }
16
- else if (global) {
17
- Logger.silly('Using process.env as global');
18
- rval = global;
19
- }
20
- if (!rval) {
21
- if (returnNullOnNone) {
22
- Logger.silly('Returning null as global');
23
- rval = null;
24
- }
25
- else {
26
- throw ErrorRatchet.fErr('Could not find process.env OR global in scope');
27
- }
28
- }
29
- return rval;
30
- }
31
- static fetchGlobalVar(envVar) {
32
- RequireRatchet.notNullOrUndefined(envVar, 'envVar');
33
- const myGlobal = GlobalRatchet.fetchGlobalRecord();
34
- const value = myGlobal[envVar];
35
- return value;
36
- }
37
- static setGlobalVar(envVar, value) {
38
- RequireRatchet.notNullOrUndefined(envVar, 'envVar');
39
- RequireRatchet.notNullOrUndefined(value, 'value');
40
- const myGlobal = GlobalRatchet.fetchGlobalRecord();
41
- myGlobal[envVar] = value;
42
- }
43
- }
@@ -1,14 +0,0 @@
1
- import { GlobalRatchet } from './global-ratchet.js';
2
- import { StringRatchet } from './string-ratchet.js';
3
- describe('#globalRatchet', function () {
4
- it('fetch and set global variables', async () => {
5
- const tester = StringRatchet.createType4Guid();
6
- const tester2 = StringRatchet.createType4Guid();
7
- process.env['GLOBALTEST'] = tester;
8
- const value = GlobalRatchet.fetchGlobalVar('GLOBALTEST');
9
- expect(value).toEqual(tester);
10
- GlobalRatchet.setGlobalVar('GLOBALTEST', tester2);
11
- const value2 = GlobalRatchet.fetchGlobalVar('GLOBALTEST');
12
- expect(value2).toEqual(tester2);
13
- });
14
- });
@@ -1,4 +0,0 @@
1
- export class KeyValue {
2
- key;
3
- value;
4
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,164 +0,0 @@
1
- import { Logger } from '../logger/logger.js';
2
- import { ErrorRatchet } from './error-ratchet.js';
3
- import { set } from 'lodash-es';
4
- export class MapRatchet {
5
- static expandNestedKeys(src) {
6
- const rval = {};
7
- Object.keys(src).forEach((k) => {
8
- set(rval, k, src[k]);
9
- });
10
- return rval;
11
- }
12
- static mapByUniqueProperty(input, propName) {
13
- if (!input || !propName) {
14
- throw new Error('Neither input nor propName can be null');
15
- }
16
- const rval = new Map();
17
- input.forEach((i) => {
18
- const val = !!i ? i[propName] : null;
19
- if (val === null || val === undefined) {
20
- throw new Error('No value for ' + propName + ' found in ' + JSON.stringify(i));
21
- }
22
- if (rval.has(val)) {
23
- throw new Error('Multiple values found for ' + val);
24
- }
25
- rval.set(val, i);
26
- });
27
- return rval;
28
- }
29
- static groupByProperty(input, propName) {
30
- if (!input || !propName) {
31
- throw new Error('Neither input nor propName can be null');
32
- }
33
- const rval = new Map();
34
- input.forEach((i) => {
35
- const val = !!i ? i[propName] : null;
36
- if (val === null || val === undefined) {
37
- throw ErrorRatchet.fErr('No value for %s found in %j', propName, i);
38
- }
39
- if (!rval.has(val)) {
40
- rval.set(val, []);
41
- }
42
- rval.get(val).push(i);
43
- });
44
- return rval;
45
- }
46
- static findValue(toSearch, path) {
47
- if (!path || path.length == 0) {
48
- return toSearch;
49
- }
50
- else {
51
- if (toSearch) {
52
- return MapRatchet.findValue(toSearch[path[0]], path.slice(1));
53
- }
54
- else {
55
- return null;
56
- }
57
- }
58
- }
59
- static findValueDotPath(toSearch, dotPath) {
60
- if (!dotPath || dotPath.length == 0) {
61
- return toSearch;
62
- }
63
- else {
64
- if (toSearch) {
65
- return MapRatchet.findValue(toSearch, dotPath.split('.'));
66
- }
67
- else {
68
- return null;
69
- }
70
- }
71
- }
72
- static simpleDeepCompare(object1, object2) {
73
- if (object1 == null && object2 == null)
74
- return true;
75
- if (object1 == null || object2 == null)
76
- return false;
77
- return JSON.stringify(object1) == JSON.stringify(object2);
78
- }
79
- static toKeyValueList(value) {
80
- const returnArray = [];
81
- for (const k of Object.keys(value)) {
82
- returnArray.push({
83
- key: k,
84
- value: value[k],
85
- });
86
- }
87
- return returnArray;
88
- }
89
- static fromKeyValueList(list) {
90
- const rval = {};
91
- list.forEach((a) => (rval[a.key] = a.value));
92
- return rval;
93
- }
94
- static cleanup(obj, stripZero = false, stripNull = true, stripUndefined = true, stripEmptyString = true) {
95
- if (obj === null || obj === undefined || typeof obj !== 'object') {
96
- return obj;
97
- }
98
- const o = JSON.parse(JSON.stringify(obj));
99
- Object.keys(o).forEach((key) => {
100
- if (o[key] && typeof o[key] === 'object') {
101
- if (Array.isArray(o[key])) {
102
- for (let i = 0; i < o[key].length; i++) {
103
- o[key][i] = MapRatchet.cleanup(o[key][i]);
104
- }
105
- }
106
- else {
107
- o[key] = MapRatchet.cleanup(o[key]);
108
- }
109
- }
110
- else if ((o[key] === undefined && stripUndefined) ||
111
- (o[key] === null && stripNull) ||
112
- (o[key] === '' && stripEmptyString) ||
113
- (o[key] === 0 && stripZero)) {
114
- delete o[key];
115
- }
116
- else {
117
- o[key] = o[key];
118
- }
119
- });
120
- return o;
121
- }
122
- static extractValueFromMapIgnoreCase(src, key) {
123
- let rval = null;
124
- if (src && key) {
125
- const finder = key.toLowerCase();
126
- Object.keys(src).forEach((s) => {
127
- if (s.toLowerCase() === finder) {
128
- const newVal = src[s];
129
- if (rval) {
130
- Logger.warn('Multiple entries found for %s (replacing %s with %s', key, rval, newVal);
131
- }
132
- rval = newVal;
133
- }
134
- });
135
- }
136
- return rval;
137
- }
138
- static safeCallFunction(ob, fnName) {
139
- let rval = false;
140
- if (!!ob && !!ob[fnName] && typeof ob[fnName] === 'function') {
141
- try {
142
- ob[fnName]();
143
- rval = true;
144
- }
145
- catch (err) {
146
- Logger.warn('Error calling %s on %s : %s', fnName, ob, err, err);
147
- }
148
- }
149
- return rval;
150
- }
151
- static caseInsensitiveAccess(ob, keyName) {
152
- let rval = null;
153
- if (!!ob && !!keyName) {
154
- rval = ob[keyName];
155
- if (!rval) {
156
- const keyNameCI = Object.keys(ob).find((f) => f.toLowerCase() === keyName.toLowerCase());
157
- if (!!keyNameCI) {
158
- rval = ob[keyNameCI];
159
- }
160
- }
161
- }
162
- return rval;
163
- }
164
- }
@@ -1,90 +0,0 @@
1
- import { MapRatchet } from './map-ratchet.js';
2
- describe('#toKeyValueList', function () {
3
- it('should expand a maps nested keys', function () {
4
- const test = { 'a.b': 'c', 'c.d[0].e': 2 };
5
- const out = MapRatchet.expandNestedKeys(test);
6
- expect(out).toBeTruthy();
7
- expect(out.a).toBeTruthy();
8
- expect(out.a.b).toBeTruthy();
9
- expect(out.a.b).toEqual('c');
10
- expect(out.c).toBeTruthy();
11
- expect(out.c.d).toBeTruthy();
12
- expect(out.c.d.length).toEqual(1);
13
- expect(out.c.d[0].e).toBeTruthy();
14
- expect(out.c.d[0].e).toEqual(2);
15
- });
16
- it('should convert a map to a key-value list and back', function () {
17
- const test = { key1: 'value1', key2: 'value2' };
18
- const kvl = MapRatchet.toKeyValueList(test);
19
- expect(kvl.length).toEqual(2);
20
- const back = MapRatchet.fromKeyValueList(kvl);
21
- expect(back['key1']).toEqual('value1');
22
- expect(back['key2']).toEqual('value2');
23
- });
24
- it('should remove null and empty strings but not 0', function () {
25
- const test = {
26
- key1: '',
27
- key2: null,
28
- key3: 0,
29
- key4: 'fine',
30
- key5: {
31
- key6: null,
32
- key7: 'also_fine',
33
- },
34
- };
35
- const clean = MapRatchet.cleanup(test);
36
- expect(Object.keys(clean).length).toEqual(3);
37
- expect(Object.keys(clean).indexOf('key1')).toEqual(-1);
38
- expect(Object.keys(clean).indexOf('key2')).toEqual(-1);
39
- expect(Object.keys(clean).indexOf('key3')).toBeGreaterThan(-1);
40
- expect(Object.keys(clean).indexOf('key4')).toBeGreaterThan(-1);
41
- expect(Object.keys(clean).indexOf('key5')).toBeGreaterThan(-1);
42
- expect(clean['key5']['key7']).toEqual('also_fine');
43
- });
44
- it('should extract values ignoring case', function () {
45
- const test = {
46
- 'this-Test': 'a',
47
- test22: 'b',
48
- };
49
- expect(MapRatchet.extractValueFromMapIgnoreCase(test, 'THIS-TEST')).toEqual('a');
50
- expect(MapRatchet.extractValueFromMapIgnoreCase(test, 'tEst22')).toEqual('b');
51
- });
52
- it('should group values', function () {
53
- const test = [
54
- {
55
- id: 1,
56
- type: 'a',
57
- },
58
- {
59
- id: 2,
60
- type: 'a',
61
- },
62
- {
63
- id: 3,
64
- type: 'b',
65
- },
66
- ];
67
- const grouped = MapRatchet.groupByProperty(test, 'type');
68
- expect(grouped).toBeTruthy();
69
- const aGroup = grouped.get('a');
70
- expect(aGroup).toBeTruthy();
71
- expect(aGroup.length).toEqual(2);
72
- expect(grouped.has('b')).toBeTruthy();
73
- expect(grouped.has('c')).toBeFalsy();
74
- });
75
- it('should fetch a value with a case-insensitive key', function () {
76
- const testOb = {
77
- test1: 'a',
78
- Test2: 'b',
79
- test3: 'c',
80
- Test3: 'd',
81
- };
82
- expect(MapRatchet.caseInsensitiveAccess(null, null)).toBeNull();
83
- expect(MapRatchet.caseInsensitiveAccess({}, null)).toBeNull();
84
- expect(MapRatchet.caseInsensitiveAccess(null, 'a')).toBeNull();
85
- expect(MapRatchet.caseInsensitiveAccess(testOb, 'test1')).toEqual('a');
86
- expect(MapRatchet.caseInsensitiveAccess(testOb, 'test2')).toEqual('b');
87
- expect(MapRatchet.caseInsensitiveAccess(testOb, 'TEST2')).toEqual('b');
88
- expect(MapRatchet.caseInsensitiveAccess(testOb, 'test3')).toEqual('c');
89
- });
90
- });
package/lib/lang/no.js DELETED
@@ -1,4 +0,0 @@
1
- export class No {
2
- static op(...args) {
3
- }
4
- }
@@ -1,7 +0,0 @@
1
- import { PromiseRatchet } from './promise-ratchet.js';
2
- import { No } from './no.js';
3
- describe('#no', function () {
4
- it('should do nothing', () => {
5
- PromiseRatchet.wait(100).then(No.op);
6
- });
7
- });
@@ -1,155 +0,0 @@
1
- import { Logger } from '../logger/logger.js';
2
- import { RequireRatchet } from './require-ratchet.js';
3
- export class NumberRatchet {
4
- static MAX_LEADING_ZEROS_FORMAT_LENGTH = 1000;
5
- static toFixedDecimalNumber(input, placesAfterPoint) {
6
- const v = NumberRatchet.safeNumber(input);
7
- return v === null || v === undefined ? v : NumberRatchet.safeNumber(v.toFixed(placesAfterPoint));
8
- }
9
- static leadingZeros(val, size) {
10
- const sVal = String(val);
11
- if (sVal.length < size) {
12
- let pad = '0000';
13
- if (size > NumberRatchet.MAX_LEADING_ZEROS_FORMAT_LENGTH) {
14
- throw 'Cannot format number that large (max length is ' + NumberRatchet.MAX_LEADING_ZEROS_FORMAT_LENGTH + ')';
15
- }
16
- while (pad.length < size) {
17
- pad = pad + pad;
18
- }
19
- return (pad + sVal).slice(-1 * size);
20
- }
21
- else {
22
- return sVal;
23
- }
24
- }
25
- static between(inTest, inP1, inP2) {
26
- const test = NumberRatchet.safeNumber(inTest);
27
- const p1 = NumberRatchet.safeNumber(inP1);
28
- const p2 = NumberRatchet.safeNumber(inP2);
29
- return (test >= p1 && test <= p2) || (test >= p2 && test <= p1);
30
- }
31
- static safeNumber(input, ifNotNumber = null, useDefaultForNullAndUndefined) {
32
- let rval = undefined;
33
- if (input !== null && input !== undefined) {
34
- const type = typeof input;
35
- if (type == 'number') {
36
- rval = input;
37
- }
38
- else if (type == 'string') {
39
- if (input.trim().length === 0) {
40
- rval = ifNotNumber;
41
- }
42
- else {
43
- rval = Number.parseFloat(input);
44
- }
45
- }
46
- else {
47
- Logger.warn('Value is of type %s, returning default', type);
48
- rval = ifNotNumber;
49
- }
50
- if (isNaN(rval)) {
51
- Logger.debug('Parsed string to NaN - using NaN value from param');
52
- rval = ifNotNumber;
53
- }
54
- }
55
- else {
56
- rval = useDefaultForNullAndUndefined ? ifNotNumber : input;
57
- }
58
- return rval;
59
- }
60
- static numberCSVToList(inputCSV) {
61
- let rval = null;
62
- if (inputCSV) {
63
- rval = inputCSV.split(',').map((s) => {
64
- return NumberRatchet.safeNumber(s.trim());
65
- });
66
- rval = rval.filter((r) => typeof r === 'number' && !isNaN(r));
67
- }
68
- return rval;
69
- }
70
- static fitToWindow(val, b1, b2) {
71
- let rval = val;
72
- if (val === null || b1 === null || b2 === null || b1 < 0 || b2 < 0 || val < 0) {
73
- throw new Error('All values must be non-null and larger than 0');
74
- }
75
- const low = Math.min(b1, b2);
76
- const high = Math.max(b1, b2);
77
- const windowSize = high - low;
78
- if (high === low) {
79
- rval = high;
80
- }
81
- else {
82
- while (rval < low) {
83
- rval += windowSize;
84
- }
85
- while (rval > high) {
86
- rval -= windowSize;
87
- }
88
- }
89
- return rval;
90
- }
91
- static groupNumbersIntoContiguousRanges(inputData, minRangeSize = 3) {
92
- RequireRatchet.notNullOrUndefined(inputData);
93
- const input = Object.assign([], inputData);
94
- input.sort((a, b) => a - b);
95
- const singles = [];
96
- const ranges = [];
97
- let start = 0;
98
- for (let i = 1; i < input.length; i++) {
99
- if (input[i] === input[i - 1] + 1) {
100
- }
101
- else {
102
- if (start === i - 1) {
103
- singles.push(input[i - 1]);
104
- }
105
- else {
106
- const rangeSize = i - start;
107
- if (rangeSize < minRangeSize) {
108
- for (let j = start; j < i; j++) {
109
- singles.push(input[j]);
110
- }
111
- }
112
- else {
113
- ranges.push({
114
- min: input[start],
115
- max: input[i - 1],
116
- });
117
- }
118
- }
119
- start = i;
120
- }
121
- }
122
- return {
123
- singles: singles,
124
- ranges: ranges,
125
- };
126
- }
127
- static distributeItemsEvenly(items, buckets) {
128
- RequireRatchet.notNullOrUndefined(items, 'items');
129
- RequireRatchet.notNullOrUndefined(buckets, 'buckets');
130
- RequireRatchet.true(items > 0 && items === Math.floor(items), 'Items integer larger than 0');
131
- RequireRatchet.true(buckets > 0 && buckets === Math.floor(buckets), 'Buckets integer larger than 0');
132
- const offset = buckets / items;
133
- const rval = [];
134
- for (let i = 0; i < buckets; i++) {
135
- rval.push(0);
136
- }
137
- let loc = 0;
138
- let rem = items;
139
- while (rem > 0) {
140
- rval[Math.floor(loc) % buckets]++;
141
- rem--;
142
- loc += offset;
143
- }
144
- return rval;
145
- }
146
- static createRange(minInclusive, maxExclusive, step = 1) {
147
- const rval = [];
148
- let val = minInclusive;
149
- while (val < maxExclusive) {
150
- rval.push(val);
151
- val += step;
152
- }
153
- return rval;
154
- }
155
- }
@@ -1,118 +0,0 @@
1
- import { NumberRatchet } from './number-ratchet.js';
2
- import fs from 'fs';
3
- import path from 'path';
4
- import { EsmRatchet } from './esm-ratchet.js';
5
- describe('#toFixedDecimalNumber', function () {
6
- it('should convert "5.1234 to 5.12', function () {
7
- expect(NumberRatchet.toFixedDecimalNumber(5.1234, 2)).toEqual(5.12);
8
- expect(NumberRatchet.toFixedDecimalNumber('5.1234', 2)).toEqual(5.12);
9
- });
10
- it('should convert "5.5678 to 5.57', function () {
11
- expect(NumberRatchet.toFixedDecimalNumber(5.5678, 2)).toEqual(5.57);
12
- expect(NumberRatchet.toFixedDecimalNumber('5.5678', 2)).toEqual(5.57);
13
- });
14
- });
15
- describe('#leadingZeros', function () {
16
- it('should convert "5" to 05', function () {
17
- const result = NumberRatchet.leadingZeros('5', 2);
18
- expect(result).toEqual('05');
19
- });
20
- it('should leave 166 alone', function () {
21
- const result = NumberRatchet.leadingZeros('166', 2);
22
- expect(result).toEqual('166');
23
- });
24
- });
25
- describe('#safeToNumber', function () {
26
- it('should convert "55" to 55', function () {
27
- const result = NumberRatchet.safeNumber('55');
28
- expect(result).toEqual(55);
29
- });
30
- it('should leave 66 alone', function () {
31
- const result = NumberRatchet.safeNumber(66);
32
- expect(result).toEqual(66);
33
- });
34
- it('should return the default when it cannot parse', function () {
35
- const result = NumberRatchet.safeNumber({ test: 'test' }, 42);
36
- expect(result).toEqual(42);
37
- });
38
- it('should return the default for the empty string', function () {
39
- const result = NumberRatchet.safeNumber({ test: '' }, 42);
40
- expect(result).toEqual(42);
41
- });
42
- it('should return the default for null/undefined if set true', function () {
43
- const result = NumberRatchet.safeNumber(null, 42, true);
44
- expect(result).toEqual(42);
45
- const result2 = NumberRatchet.safeNumber(undefined, 46, true);
46
- expect(result2).toEqual(46);
47
- });
48
- it('should return the passed value for null/undefined if set false', function () {
49
- const result = NumberRatchet.safeNumber(null, 42, false);
50
- expect(result).toEqual(null);
51
- const result2 = NumberRatchet.safeNumber(undefined, 46, false);
52
- expect(result2).toEqual(undefined);
53
- });
54
- it('should return the passed value for null/undefined if not set', function () {
55
- const result = NumberRatchet.safeNumber(null, 42);
56
- expect(result).toEqual(null);
57
- const result2 = NumberRatchet.safeNumber(undefined, 46);
58
- expect(result2).toEqual(undefined);
59
- });
60
- });
61
- describe('#parseCSV', function () {
62
- it('should convert "1,2,3" to [1,2,3]', function () {
63
- const result = NumberRatchet.numberCSVToList('1,2,3');
64
- expect(result.length).toEqual(3);
65
- });
66
- it('should convert " 1, 2,3 " to [1,2,3]', function () {
67
- const result = NumberRatchet.numberCSVToList(' 1, 2,3 ');
68
- expect(result.length).toEqual(3);
69
- });
70
- it('should convert " a1, 2,b " to [2]', function () {
71
- const result = NumberRatchet.numberCSVToList(' a1, 2,b ');
72
- expect(result.length).toEqual(1);
73
- expect(result[0]).toEqual(2);
74
- });
75
- });
76
- describe('#fitToWindow', function () {
77
- it('should fit input to the window', function () {
78
- expect(NumberRatchet.fitToWindow(5, 8, 10)).toEqual(9);
79
- expect(NumberRatchet.fitToWindow(8, 2, 10)).toEqual(8);
80
- expect(NumberRatchet.fitToWindow(8, 9, 9)).toEqual(9);
81
- expect(NumberRatchet.fitToWindow(12, 2, 10)).toEqual(4);
82
- });
83
- });
84
- describe('#groupNumbersIntoContiguousRanges', function () {
85
- it('should group numbers', function () {
86
- let input = JSON.parse(fs.readFileSync(path.join(EsmRatchet.fetchDirName(import.meta.url), '../../../../test-data/number_set.json')).toString());
87
- input = input.map((i) => NumberRatchet.safeNumber(i));
88
- const grouped = NumberRatchet.groupNumbersIntoContiguousRanges(input, 5);
89
- expect(grouped).toBeTruthy();
90
- });
91
- });
92
- describe('#distributeItemsEvenly', function () {
93
- it('should distribute evenly', function () {
94
- const test1 = NumberRatchet.distributeItemsEvenly(4, 6);
95
- expect(test1).toBeTruthy();
96
- const test2 = NumberRatchet.distributeItemsEvenly(12, 6);
97
- expect(test2).toBeTruthy();
98
- const test3 = NumberRatchet.distributeItemsEvenly(5, 6);
99
- expect(test3).toBeTruthy();
100
- const test4 = NumberRatchet.distributeItemsEvenly(192, 11);
101
- expect(test4).toBeTruthy();
102
- const test5 = NumberRatchet.distributeItemsEvenly(11, 192);
103
- expect(test5).toBeTruthy();
104
- });
105
- });
106
- describe('#createRange', function () {
107
- it('should create ranges', function () {
108
- const test1 = NumberRatchet.createRange(0, 5, 1);
109
- expect(test1).toBeTruthy();
110
- expect(test1.length).toEqual(5);
111
- const test2 = NumberRatchet.createRange(1, 5, 1);
112
- expect(test2).toBeTruthy();
113
- expect(test2.length).toEqual(4);
114
- const test3 = NumberRatchet.createRange(0, 10, 2);
115
- expect(test3).toBeTruthy();
116
- expect(test3.length).toEqual(5);
117
- });
118
- });
@@ -1 +0,0 @@
1
- export {};