cno-bedrock 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/.github/workflows/ci.yml +48 -0
- package/CHANGELOG.md +33 -0
- package/LICENSE +16 -0
- package/README.md +39 -0
- package/ci/github-actions.hjson +72 -0
- package/docs/src/cno-assert.md +158 -0
- package/docs/src/cno-noop.md +29 -0
- package/docs/src/cno-options.md +61 -0
- package/docs/src/cno-time.md +145 -0
- package/docs/src/cno-utility.md +76 -0
- package/docs/src/lib.md +73 -0
- package/docs/src/lib.test.md +29 -0
- package/package.json +111 -0
- package/scripts/generate-docs.js +6 -0
- package/src/cno-assert.js +314 -0
- package/src/cno-noop.js +75 -0
- package/src/cno-options.js +108 -0
- package/src/cno-time.js +348 -0
- package/src/cno-utility.js +151 -0
- package/src/lib.js +173 -0
- package/src/lib.test.js +156 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
# [cno-options.js](src/cno-options.js)
|
|
4
|
+
> Implements functions and defaults for `input_options`-type parametres.
|
|
5
|
+
|
|
6
|
+
Author: Anadian
|
|
7
|
+
|
|
8
|
+
Code license: MIT
|
|
9
|
+
```
|
|
10
|
+
Copyright 2025 Anadian
|
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
|
12
|
+
software and associated documentation files (the "Software"), to deal in the Software
|
|
13
|
+
without restriction, including without limitation the rights to use, copy, modify,
|
|
14
|
+
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
|
|
15
|
+
permit persons to whom the Software is furnished to do so, subject to the following
|
|
16
|
+
conditions:
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all copies
|
|
18
|
+
or substantial portions of the Software.
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
20
|
+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
21
|
+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
22
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
23
|
+
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
|
24
|
+
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
25
|
+
```
|
|
26
|
+
Documentation License: [](http://creativecommons.org/licenses/by-sa/4.0/)
|
|
27
|
+
> The source-code comments and documentation are written in [GitHub Flavored Markdown](https://github.github.com/gfm/).
|
|
28
|
+
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
//# Dependencies
|
|
32
|
+
//## Internal
|
|
33
|
+
import NOOP from './cno-noop.js'
|
|
34
|
+
//## Standard
|
|
35
|
+
//## External
|
|
36
|
+
//# Constants
|
|
37
|
+
const FILENAME = 'cno-options.js';
|
|
38
|
+
//## Errors
|
|
39
|
+
|
|
40
|
+
//# Global Variables
|
|
41
|
+
/**## Functions*/
|
|
42
|
+
// Options
|
|
43
|
+
/**
|
|
44
|
+
### deriveOptions
|
|
45
|
+
> Derives and returns the final runtime options.
|
|
46
|
+
|
|
47
|
+
#### Parametres
|
|
48
|
+
| name | type | description |
|
|
49
|
+
| --- | --- | --- |
|
|
50
|
+
| input_options | object | Run-time options. |
|
|
51
|
+
| default_options | object | The default options to be derived against. |
|
|
52
|
+
| dynamic_function | function | A function which receives an options object as an argument and returns a modified options object; used for dynamic defaults derived from other options. \[default: null\] |
|
|
53
|
+
| standard_options | boolean | Whether to include the defaults for the standard options \(see below\). \[default: true\] |
|
|
54
|
+
|
|
55
|
+
##### Stanard Options
|
|
56
|
+
> Available via `deriveOptions.STANDARD_OPTIONS`.
|
|
57
|
+
| name | type | default | description |
|
|
58
|
+
| --- | --- | --- | --- |
|
|
59
|
+
| noop | boolean | false | Skip primary functionality. |
|
|
60
|
+
| noDefaults | boolean | false | Don't apply static default options. |
|
|
61
|
+
| noDynamic | boolean | false | Don't apply dynamic default options. |
|
|
62
|
+
| logFunction | function | [returnNull](#returnNull) | A function to use for logging. \[Default: [returnNULL](#returnNull)\] |
|
|
63
|
+
| validationFunction | function | [returnTrue](#returnTrue) | A function for validating the contents of the final options, returning `true` meaning things proceed. \[Default: [returnTrue](#returnTrue)\] |
|
|
64
|
+
|
|
65
|
+
#### Returns
|
|
66
|
+
| type | description |
|
|
67
|
+
| --- | --- |
|
|
68
|
+
| object | Returns the final options object. |
|
|
69
|
+
|
|
70
|
+
#### History
|
|
71
|
+
| version | change |
|
|
72
|
+
| --- | --- |
|
|
73
|
+
| 0.0.1 | WIP |
|
|
74
|
+
*/
|
|
75
|
+
function deriveOptions( input_options, default_options, dynamic_function = null, standard_options = true ){
|
|
76
|
+
var _return = null;
|
|
77
|
+
var options = {};
|
|
78
|
+
if( standard_options !== false ){
|
|
79
|
+
options = Object.assign( options, deriveOptions.STANDARD_OPTIONS, input_options );
|
|
80
|
+
}
|
|
81
|
+
if( input_options.noDefaults !== true && default_options != null ){
|
|
82
|
+
options = Object.assign( options, default_options, input_options );
|
|
83
|
+
if( input_options.noDynamic !== true && typeof(dynamic_function) === 'function' && dynamic_function != null ){
|
|
84
|
+
options = Object.assign( options, dynamic_function.call( this, options ), input_options );
|
|
85
|
+
} // noDynamic
|
|
86
|
+
} else{
|
|
87
|
+
options = Object.assign( {}, input_options );
|
|
88
|
+
} // noDefaults
|
|
89
|
+
_return = options;
|
|
90
|
+
return _return;
|
|
91
|
+
} // deriveOptions
|
|
92
|
+
Object.defineProperties( deriveOptions, {
|
|
93
|
+
STANDARD_OPTIONS: {
|
|
94
|
+
value: {
|
|
95
|
+
noop: false,
|
|
96
|
+
noDefaults: false,
|
|
97
|
+
noDynamic: false,
|
|
98
|
+
logFunction: NOOP.returnNull,
|
|
99
|
+
validationFunction: NOOP.returnTrue
|
|
100
|
+
},
|
|
101
|
+
enumerable: true
|
|
102
|
+
}
|
|
103
|
+
} );
|
|
104
|
+
|
|
105
|
+
export default deriveOptions;
|
|
106
|
+
|
|
107
|
+
// cno-options.js EOF
|
|
108
|
+
|
package/src/cno-time.js
ADDED
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
# [cno-time.js](src/cno-time.js)
|
|
4
|
+
> `cno-time`: A micropackage with time-related utilities.
|
|
5
|
+
|
|
6
|
+
Author: Anadian
|
|
7
|
+
|
|
8
|
+
Code license: MIT
|
|
9
|
+
```
|
|
10
|
+
Copyright 2025 Anadian
|
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
|
12
|
+
software and associated documentation files (the "Software"), to deal in the Software
|
|
13
|
+
without restriction, including without limitation the rights to use, copy, modify,
|
|
14
|
+
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
|
|
15
|
+
permit persons to whom the Software is furnished to do so, subject to the following
|
|
16
|
+
conditions:
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all copies
|
|
18
|
+
or substantial portions of the Software.
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
20
|
+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
21
|
+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
22
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
23
|
+
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
|
24
|
+
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
25
|
+
```
|
|
26
|
+
Documentation License: [](http://creativecommons.org/licenses/by-sa/4.0/)
|
|
27
|
+
> The source-code comments and documentation are written in [GitHub Flavored Markdown](https://github.github.com/gfm/).
|
|
28
|
+
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
//# Dependencies
|
|
32
|
+
//## Internal
|
|
33
|
+
import deriveOptions from './cno-options.js';
|
|
34
|
+
//## Standard
|
|
35
|
+
//## External
|
|
36
|
+
import * as MathJS from 'mathjs';
|
|
37
|
+
//# Constants
|
|
38
|
+
const FILENAME = 'cno-time.js';
|
|
39
|
+
|
|
40
|
+
//## Errors
|
|
41
|
+
|
|
42
|
+
//# Global Variables
|
|
43
|
+
/**## Functions*/
|
|
44
|
+
/**
|
|
45
|
+
### getNowISO
|
|
46
|
+
> Returns 'now' as an ISO 8601 string.
|
|
47
|
+
|
|
48
|
+
#### Returns
|
|
49
|
+
| type | description |
|
|
50
|
+
| --- | --- |
|
|
51
|
+
| string | The ISO8601/RFC3339-formatted string. |
|
|
52
|
+
|
|
53
|
+
#### History
|
|
54
|
+
| version | change |
|
|
55
|
+
| --- | --- |
|
|
56
|
+
| 0.0.1 | WIP |
|
|
57
|
+
*/
|
|
58
|
+
function getNowISO(){
|
|
59
|
+
var _return = new Date();
|
|
60
|
+
return _return.toISOString();
|
|
61
|
+
} // getNowISO
|
|
62
|
+
/**
|
|
63
|
+
### getDateFromUnixTimestamp
|
|
64
|
+
> Returns a new [Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) from a Unix-style millisecond timestamp.
|
|
65
|
+
|
|
66
|
+
#### Parametres
|
|
67
|
+
| name | type | description |
|
|
68
|
+
| --- | --- | --- |
|
|
69
|
+
| unix_timestamp | number | Timestamp in milliseconds. |
|
|
70
|
+
|
|
71
|
+
#### Returns
|
|
72
|
+
| type | description |
|
|
73
|
+
| --- | --- |
|
|
74
|
+
| Date | The new Date object. |
|
|
75
|
+
|
|
76
|
+
#### History
|
|
77
|
+
| version | change |
|
|
78
|
+
| --- | --- |
|
|
79
|
+
| 0.0.1 | WIP |
|
|
80
|
+
*/
|
|
81
|
+
function getDateFromUnixTimestamp( unix_timestamp ){
|
|
82
|
+
var _return = new Date( unix_timestamp * 1000 );
|
|
83
|
+
return _return;
|
|
84
|
+
} // getDateFromUnixTimestamp
|
|
85
|
+
/**
|
|
86
|
+
### getISOStringFromUnixTimestamp
|
|
87
|
+
> Returns an ISO string from a Unix-style millisecond timestamp.
|
|
88
|
+
|
|
89
|
+
#### Parametres
|
|
90
|
+
| name | type | description |
|
|
91
|
+
| --- | --- | --- |
|
|
92
|
+
| unix_timestamp | number | Timestamp in milliseconds. |
|
|
93
|
+
|
|
94
|
+
#### Returns
|
|
95
|
+
| type | description |
|
|
96
|
+
| --- | --- |
|
|
97
|
+
| string | The ISO8601/RFC3339 string. |
|
|
98
|
+
|
|
99
|
+
#### History
|
|
100
|
+
| version | change |
|
|
101
|
+
| --- | --- |
|
|
102
|
+
| 0.0.1 | WIP |
|
|
103
|
+
*/
|
|
104
|
+
function getISOStringFromUnixTimestamp( unix_timestamp ){
|
|
105
|
+
return getDateFromUnixTimestamp( unix_timestamp ).toISOString();
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
### Clock
|
|
109
|
+
> Creates a new clock object.
|
|
110
|
+
|
|
111
|
+
#### Parametres
|
|
112
|
+
| name | type | description |
|
|
113
|
+
| --- | --- | --- |
|
|
114
|
+
| input_options | object | Run-time options. \[default: {}\] |
|
|
115
|
+
|
|
116
|
+
##### `options` Properties
|
|
117
|
+
| name | type | default | description |
|
|
118
|
+
| --- | --- | --- | --- |
|
|
119
|
+
| getFunction | function | process.hrtime.bigint | The function to use to get the current timestamp. |
|
|
120
|
+
| granularity | number | 0.000000001 | A number representing the precision of the clock get function relative to seconds. |
|
|
121
|
+
|
|
122
|
+
#### Returns
|
|
123
|
+
| type | description |
|
|
124
|
+
| --- | --- |
|
|
125
|
+
| object | Returns the new Clock object. |
|
|
126
|
+
|
|
127
|
+
#### Throws
|
|
128
|
+
| code | type | condition |
|
|
129
|
+
| --- | --- | --- |
|
|
130
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
|
|
131
|
+
|
|
132
|
+
#### History
|
|
133
|
+
| version | change |
|
|
134
|
+
| --- | --- |
|
|
135
|
+
| 0.0.1 | WIP |
|
|
136
|
+
*/
|
|
137
|
+
function Clock( input_options = {} ){
|
|
138
|
+
if( !new.target ){
|
|
139
|
+
return new Clock(input_options, );
|
|
140
|
+
}
|
|
141
|
+
const FUNCTION_NAME = 'Clock';
|
|
142
|
+
const DEFAULT_OPTIONS = {
|
|
143
|
+
getFunction: process.hrtime.bigint, // The function to use to get the current timestamp.
|
|
144
|
+
granularity: 0.000000001, // A number representing the precision of the clock get function relative to seconds.
|
|
145
|
+
};// Variables
|
|
146
|
+
var _return = null;
|
|
147
|
+
var return_error = null;
|
|
148
|
+
var options = {};
|
|
149
|
+
// Parametre checks
|
|
150
|
+
if( typeof(input_options) !== 'object' ){
|
|
151
|
+
return_error = new TypeError('Param "input_options" is not of type object.');
|
|
152
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
153
|
+
throw return_error;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Options
|
|
157
|
+
var { logFunction: log_function = this?.logger?.log, validationFunction: validation_function, ...options } = deriveOptions( input_options, DEFAULT_OPTIONS );
|
|
158
|
+
if( validation_function( options ) === true ){
|
|
159
|
+
if( options.noop !== true ){
|
|
160
|
+
// Function
|
|
161
|
+
Object.defineProperties( this, {
|
|
162
|
+
getFunction: {
|
|
163
|
+
value: options.getFunction,
|
|
164
|
+
enumerable: true
|
|
165
|
+
},
|
|
166
|
+
granularity: {
|
|
167
|
+
value: options.granularity,
|
|
168
|
+
enumerable: true
|
|
169
|
+
}
|
|
170
|
+
} );
|
|
171
|
+
} // noop
|
|
172
|
+
} // validation_function
|
|
173
|
+
// Return
|
|
174
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `returned: ${_return}`});
|
|
175
|
+
return _return;
|
|
176
|
+
} // Clock
|
|
177
|
+
|
|
178
|
+
// Benmark
|
|
179
|
+
function Benchmark( options = null ){
|
|
180
|
+
if( !new.target ){
|
|
181
|
+
return new Benchmark( options );
|
|
182
|
+
}
|
|
183
|
+
this.clock ??= options?.clock ?? new Clock();
|
|
184
|
+
this.now ??= options?.now ?? this.clock.getFunction;
|
|
185
|
+
this.granularity ??= options?.granularity ?? this.clock.granularity;
|
|
186
|
+
this.marks ??= options?.marks ?? 0;
|
|
187
|
+
this.intervals ??= options?.intervals ?? 0;
|
|
188
|
+
this.markArray ??= options?.mark_array ?? [];
|
|
189
|
+
this.duration ??= options?.duration ?? 0;
|
|
190
|
+
return this;
|
|
191
|
+
}
|
|
192
|
+
Benchmark.prototype.begin = function(){
|
|
193
|
+
this.markArray = [];
|
|
194
|
+
this.marks = 1;
|
|
195
|
+
this.markArray.push( this.now() );
|
|
196
|
+
}
|
|
197
|
+
Benchmark.prototype.mark = function(){
|
|
198
|
+
this.markArray.push( this.now() );
|
|
199
|
+
this.marks++;
|
|
200
|
+
}
|
|
201
|
+
Benchmark.prototype.end = function(){
|
|
202
|
+
this.mark();
|
|
203
|
+
this.intervals = this.marks - 1;
|
|
204
|
+
this.duration = this.markArray[this.intervals] - this.markArray[0];
|
|
205
|
+
}
|
|
206
|
+
Benchmark.prototype.getIntervals = function(){
|
|
207
|
+
var _return = [];
|
|
208
|
+
for( var i = 0; i < this.intervals; i++ ){
|
|
209
|
+
_return.push( this.markArray[i+1] - this.markArray[i] );
|
|
210
|
+
}
|
|
211
|
+
return _return;
|
|
212
|
+
}
|
|
213
|
+
// Duration
|
|
214
|
+
/**
|
|
215
|
+
### getStringFromDurationOptions
|
|
216
|
+
> Returns a formatted string depending on the options specified.
|
|
217
|
+
|
|
218
|
+
#### Parametres
|
|
219
|
+
| name | type | description |
|
|
220
|
+
| --- | --- | --- |
|
|
221
|
+
| input_options | object | Run-time options. \[default: {}\] |
|
|
222
|
+
|
|
223
|
+
##### `options` Properties
|
|
224
|
+
| name | type | default | description |
|
|
225
|
+
| --- | --- | --- | --- |
|
|
226
|
+
| duration | number | Number.NaN | The duration; assumed to be in seconds unless 'duration_unit' is specified. |
|
|
227
|
+
| unit_seperator | string|boolean | "\u205f" | A string to place between a number and its unit or a non-truthy value to omit units entirely from the formatted string. |
|
|
228
|
+
| split_separator | string|boolean | "\u2005" | A string to put between each order of magnitude or a non-truthy value to omit separation altogether. |
|
|
229
|
+
| duration_unit | object\|string | `MathJS.unit('s')` | A valid [MathJS unit object](https://mathjs.org/docs/datatypes/units.html#units) such as the one returned from the `unit` function, or a string to be passed to said function. |
|
|
230
|
+
| split_units | Array | `[ 'h', 'min', 's' ]` | An array to be passed to [`splitUnit`](https://mathjs.org/docs/datatypes/units.html#unitsplitunitparts). See [MathJS](https://mathjs.org/docs/datatypes/units.html#reference) for available units. |
|
|
231
|
+
|
|
232
|
+
#### Returns
|
|
233
|
+
| type | description |
|
|
234
|
+
| --- | --- |
|
|
235
|
+
| string | The formatted duration string. |
|
|
236
|
+
|
|
237
|
+
#### Throws
|
|
238
|
+
| code | type | condition |
|
|
239
|
+
| --- | --- | --- |
|
|
240
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
|
|
241
|
+
|
|
242
|
+
#### History
|
|
243
|
+
| version | change |
|
|
244
|
+
| --- | --- |
|
|
245
|
+
| 0.0.1 | WIP |
|
|
246
|
+
*/
|
|
247
|
+
function getStringFromDurationOptions( input_options = {} ){
|
|
248
|
+
const FUNCTION_NAME = 'getStringFromDurationOptions';
|
|
249
|
+
const DEFAULT_OPTIONS = {
|
|
250
|
+
duration: Number.NaN, // The duration; assumed to be in seconds unless `base` is specified.
|
|
251
|
+
//seconds_per_unit: 1, // The base of scale for `duration`; for example: `1` mean duration is specified in seconds, `0.001` means duration is in milliseconds, `60` means duration is in minutes.
|
|
252
|
+
//units_per_second: 1, // The inverse of `seconds_per_unit` which can also be used to derive `seconds_per_unit` via taking the reciprocal (1/`units_per_second`); ignored if `seconds_per_unit` is specified.
|
|
253
|
+
unit_separator: "\u205f", // A string to place between a number and its unit or a non-truthy value to omit units entirely from the formatted string.
|
|
254
|
+
split_separator: "\u2005", // A string to put between each order of magnitude or a non-truthy value to omit separation altogether.
|
|
255
|
+
//applied_units: { hours: 'h', minutes: 'min', seconds: 's' }, // An object whose properties indicate which units should be included with the property value indicating a preferred unit symbol. See [MathJS](https://mathjs.org/docs/datatypes/units.html#reference) for available units.
|
|
256
|
+
duration_unit: MathJS.unit('s'),
|
|
257
|
+
split_units: [ 'h', 'min', 's' ]
|
|
258
|
+
};// Variables
|
|
259
|
+
var _return = '';
|
|
260
|
+
var return_error = null;
|
|
261
|
+
var options = {};
|
|
262
|
+
// Parametre checks
|
|
263
|
+
if( typeof(input_options) !== 'object' ){
|
|
264
|
+
return_error = new TypeError('Param "input_options" is not of type object.');
|
|
265
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
266
|
+
throw return_error;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// Options
|
|
270
|
+
var { logFunction: log_function = this?.logger?.log, validationFunction: validation_function, ...options } = deriveOptions( input_options, DEFAULT_OPTIONS, ( options ) => {
|
|
271
|
+
/* c8 ignore start */
|
|
272
|
+
/*if( !Number.isFinite( options.seconds_per_unit ) ){
|
|
273
|
+
if( Number.isFinite( options.units_per_second ) ){
|
|
274
|
+
options.seconds_per_unit = 1/options.units_per_second;
|
|
275
|
+
}
|
|
276
|
+
}*/
|
|
277
|
+
if( typeof(options.duration_unit) === 'string' ){
|
|
278
|
+
options.duration_unit = MathJS.unit( options.duration_unit );
|
|
279
|
+
}
|
|
280
|
+
/* c8 ignore stop */
|
|
281
|
+
return options;
|
|
282
|
+
} );
|
|
283
|
+
if( validation_function( options ) === true ){
|
|
284
|
+
if( options.noop !== true ){
|
|
285
|
+
// Function
|
|
286
|
+
var working_duration_unit = MathJS.unit( options.duration, options.duration_unit );
|
|
287
|
+
var split_array = working_duration_unit.splitUnit( options.split_units );
|
|
288
|
+
var output_array = [];
|
|
289
|
+
for( var part_unit of split_array ){
|
|
290
|
+
var part_string = part_unit.toString();
|
|
291
|
+
//console.log( part_string );
|
|
292
|
+
if( options.unit_separator ){
|
|
293
|
+
//console.log( 'unit_separator truthy' );
|
|
294
|
+
part_string = part_string.replace( ' ', options.unit_separator );
|
|
295
|
+
}
|
|
296
|
+
//console.log( "Fixed? part_string: %s", part_string );
|
|
297
|
+
output_array.push( part_string );
|
|
298
|
+
}
|
|
299
|
+
if( options.split_separator ){
|
|
300
|
+
_return = output_array.join( options.split_separator );
|
|
301
|
+
} else /* c8 ignore start */ {
|
|
302
|
+
_return = output_array.join('');
|
|
303
|
+
} /* c8 ignore stop */
|
|
304
|
+
/*var sorted_units = [];
|
|
305
|
+
for( const unit_key of Object.keys(options.applied_units) ){
|
|
306
|
+
var unit_value = MathJS.unit(1, unit_key);
|
|
307
|
+
unit_value.to('s');
|
|
308
|
+
}*/
|
|
309
|
+
|
|
310
|
+
} // noop
|
|
311
|
+
} // validation_function
|
|
312
|
+
// Return
|
|
313
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `returned: ${_return}`});
|
|
314
|
+
return _return;
|
|
315
|
+
} // getStringFromDurationOptions
|
|
316
|
+
|
|
317
|
+
const NAMESPACE = {};
|
|
318
|
+
Object.defineProperties( NAMESPACE, {
|
|
319
|
+
getNowISO: {
|
|
320
|
+
value: getNowISO,
|
|
321
|
+
enumerable: true
|
|
322
|
+
},
|
|
323
|
+
getDateFromUnixTimestamp: {
|
|
324
|
+
value: getDateFromUnixTimestamp,
|
|
325
|
+
enumerable: true
|
|
326
|
+
},
|
|
327
|
+
getISOStringFromUnixTimestamp: {
|
|
328
|
+
value: getISOStringFromUnixTimestamp,
|
|
329
|
+
enumerable: true
|
|
330
|
+
},
|
|
331
|
+
Clock: {
|
|
332
|
+
value: Clock,
|
|
333
|
+
enumerable: true
|
|
334
|
+
},
|
|
335
|
+
Benchmark: {
|
|
336
|
+
value: Benchmark,
|
|
337
|
+
enumerable: true
|
|
338
|
+
},
|
|
339
|
+
getStringFromDurationOptions: {
|
|
340
|
+
value: getStringFromDurationOptions,
|
|
341
|
+
enumerable: true
|
|
342
|
+
}
|
|
343
|
+
} );
|
|
344
|
+
|
|
345
|
+
export { NAMESPACE as default, getNowISO, getDateFromUnixTimestamp, getISOStringFromUnixTimestamp, Clock, Benchmark, getStringFromDurationOptions };
|
|
346
|
+
|
|
347
|
+
// lib.js EOF
|
|
348
|
+
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
# [cno-utility.js](src/cno-utility.js)
|
|
4
|
+
> Various utilities for debugging and common tasks.
|
|
5
|
+
|
|
6
|
+
Author: Anadian
|
|
7
|
+
|
|
8
|
+
Code license: MIT
|
|
9
|
+
```
|
|
10
|
+
Copyright 2024 Anadian
|
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
|
12
|
+
software and associated documentation files (the "Software"), to deal in the Software
|
|
13
|
+
without restriction, including without limitation the rights to use, copy, modify,
|
|
14
|
+
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
|
|
15
|
+
permit persons to whom the Software is furnished to do so, subject to the following
|
|
16
|
+
conditions:
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all copies
|
|
18
|
+
or substantial portions of the Software.
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
20
|
+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
21
|
+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
22
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
23
|
+
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
|
24
|
+
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
25
|
+
```
|
|
26
|
+
Documentation License: [](http://creativecommons.org/licenses/by-sa/4.0/)
|
|
27
|
+
> The source-code comments and documentation are written in [GitHub Flavored Markdown](https://github.github.com/gfm/).
|
|
28
|
+
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
//# Dependencies
|
|
32
|
+
//## Internal
|
|
33
|
+
import { getNowISO } from './cno-time.js';
|
|
34
|
+
//## Standard
|
|
35
|
+
import { inspect } from 'node:util';
|
|
36
|
+
//## External
|
|
37
|
+
import { nanoid } from 'nanoid';
|
|
38
|
+
//# Constants
|
|
39
|
+
const FILENAME = 'cno-utility.js';
|
|
40
|
+
//## Errors
|
|
41
|
+
|
|
42
|
+
//# Global Variables
|
|
43
|
+
/**## Functions*/
|
|
44
|
+
/**
|
|
45
|
+
### annotateThis
|
|
46
|
+
> Adds some basic information, UID and creation time, to `this`, useful for debugging.
|
|
47
|
+
|
|
48
|
+
#### History
|
|
49
|
+
| version | change |
|
|
50
|
+
| --- | --- |
|
|
51
|
+
| 0.0.1 | WIP |
|
|
52
|
+
*/
|
|
53
|
+
function annotateThis(){
|
|
54
|
+
Object.defineProperties( this, {
|
|
55
|
+
uid: {
|
|
56
|
+
value: nanoid(),
|
|
57
|
+
//configurable: false,
|
|
58
|
+
enumerable: true
|
|
59
|
+
//writable: false
|
|
60
|
+
},
|
|
61
|
+
created: {
|
|
62
|
+
value: getNowISO(),
|
|
63
|
+
enumerable: true
|
|
64
|
+
},
|
|
65
|
+
insp: {
|
|
66
|
+
value: inspThis.bind( this )
|
|
67
|
+
}
|
|
68
|
+
} );
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
### inspThis
|
|
72
|
+
> [Inspects](https://nodejs.org/api/util.html#utilinspectobject-options) `this` with some more concise defaults.
|
|
73
|
+
|
|
74
|
+
#### Parametres
|
|
75
|
+
| name | type | description |
|
|
76
|
+
| --- | --- | --- |
|
|
77
|
+
| options | object | Overwrite options to pass to inspect. \[default: null\] |
|
|
78
|
+
|
|
79
|
+
#### History
|
|
80
|
+
| version | change |
|
|
81
|
+
| --- | --- |
|
|
82
|
+
| 0.0.1 | WIP |
|
|
83
|
+
*/
|
|
84
|
+
function inspThis( options = null ){
|
|
85
|
+
options ??= { depth: 1, maxArrayLength: 16, maxStringLength: 64 };
|
|
86
|
+
return inspect( this, options );
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
### getAnnotatedObject
|
|
90
|
+
> [`annotateThis`](#annotateThis) but modifies and returns a copy of the given input object.
|
|
91
|
+
|
|
92
|
+
#### Parametres
|
|
93
|
+
| name | type | description |
|
|
94
|
+
| --- | --- | --- |
|
|
95
|
+
| input_object | object | The object to add the properties to. \[default: null\] |
|
|
96
|
+
|
|
97
|
+
#### Returns
|
|
98
|
+
| type | description |
|
|
99
|
+
| --- | --- |
|
|
100
|
+
| object | The freshly-annotated object. |
|
|
101
|
+
|
|
102
|
+
#### Throws
|
|
103
|
+
| code | type | condition |
|
|
104
|
+
| --- | --- | --- |
|
|
105
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
|
|
106
|
+
|
|
107
|
+
#### History
|
|
108
|
+
| version | change |
|
|
109
|
+
| --- | --- |
|
|
110
|
+
| 0.0.1 | WIP |
|
|
111
|
+
*/
|
|
112
|
+
function getAnnotatedObject( input_object = null ){
|
|
113
|
+
// Constants
|
|
114
|
+
const FUNCTION_NAME = 'getAnnotatedObject';
|
|
115
|
+
// Variables
|
|
116
|
+
var _return = null;
|
|
117
|
+
var return_error = null;
|
|
118
|
+
// Parametre checks
|
|
119
|
+
if( typeof(input_object) !== 'object' ){
|
|
120
|
+
return_error = new TypeError('Param "input_object" is not of type object.');
|
|
121
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
122
|
+
throw return_error;
|
|
123
|
+
}
|
|
124
|
+
// Function
|
|
125
|
+
_return = Object.assign( {}, input_object );
|
|
126
|
+
annotateThis.call( _return );
|
|
127
|
+
// Return
|
|
128
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `returned: ${_return}`});
|
|
129
|
+
return _return;
|
|
130
|
+
} // getAnnotatedObject
|
|
131
|
+
|
|
132
|
+
const NAMESPACE = {};
|
|
133
|
+
Object.defineProperties( NAMESPACE, {
|
|
134
|
+
annotateThis: {
|
|
135
|
+
value: annotateThis,
|
|
136
|
+
enumerable: true
|
|
137
|
+
},
|
|
138
|
+
inspThis: {
|
|
139
|
+
value: inspThis,
|
|
140
|
+
enumerable: true
|
|
141
|
+
},
|
|
142
|
+
getAnnotatedObject: {
|
|
143
|
+
value: getAnnotatedObject,
|
|
144
|
+
enumerable: true
|
|
145
|
+
}
|
|
146
|
+
} );
|
|
147
|
+
|
|
148
|
+
export { NAMESPACE as default, annotateThis, inspThis, getAnnotatedObject };
|
|
149
|
+
|
|
150
|
+
// cno-utility.js EOF
|
|
151
|
+
|