@douglasneuroinformatics/libjs 0.4.0 → 0.5.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/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './array.js';
2
2
  export * from './datetime.js';
3
+ export * from './number.js';
3
4
  export * from './object.js';
4
5
  export * from './random.js';
5
6
  export * from './range.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC"}
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './array.js';
2
2
  export * from './datetime.js';
3
+ export * from './number.js';
3
4
  export * from './object.js';
4
5
  export * from './random.js';
5
6
  export * from './range.js';
@@ -0,0 +1,7 @@
1
+ /** Returns `true` if the value is a string representation of a number */
2
+ export declare function isStringNumber(value: string): boolean;
3
+ /** Returns `true` if the value is a number or string representation of a number */
4
+ export declare function isNumberLike(value: unknown): value is number | string;
5
+ /** Returns `value` as a number if it is a number or string representation of a number, otherwise returns `NaN` */
6
+ export declare function parseNumber(value: unknown): number;
7
+ //# sourceMappingURL=number.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"number.d.ts","sourceRoot":"","sources":["../src/number.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,WAK3C;AAED,mFAAmF;AACnF,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,GAAG,MAAM,CAOrE;AAED,kHAAkH;AAClH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAElD"}
package/dist/number.js ADDED
@@ -0,0 +1,21 @@
1
+ /** Returns `true` if the value is a string representation of a number */
2
+ export function isStringNumber(value) {
3
+ if (value.trim() !== '') {
4
+ return !Number.isNaN(+value);
5
+ }
6
+ return false;
7
+ }
8
+ /** Returns `true` if the value is a number or string representation of a number */
9
+ export function isNumberLike(value) {
10
+ if (typeof value === 'number') {
11
+ return !Number.isNaN(value);
12
+ }
13
+ else if (typeof value === 'string') {
14
+ return isStringNumber(value);
15
+ }
16
+ return false;
17
+ }
18
+ /** Returns `value` as a number if it is a number or string representation of a number, otherwise returns `NaN` */
19
+ export function parseNumber(value) {
20
+ return isNumberLike(value) ? Number(value) : NaN;
21
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=number.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"number.test.d.ts","sourceRoot":"","sources":["../src/number.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,66 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { isNumberLike, parseNumber } from './number.js';
3
+ const NUMBER_LIKE_VALUES = [
4
+ 5e3,
5
+ 0xff,
6
+ -0.1,
7
+ -0,
8
+ 0.0,
9
+ +0,
10
+ 3.14,
11
+ '5e3',
12
+ '5e+3',
13
+ '-0.1',
14
+ '-0',
15
+ '0.0',
16
+ '+0',
17
+ '3.14',
18
+ Number.MAX_VALUE,
19
+ Number.MIN_VALUE,
20
+ Infinity,
21
+ -Infinity,
22
+ 'Infinity',
23
+ ' +Infinity',
24
+ ' -Infinity '
25
+ ];
26
+ const NON_NUMBER_LIKE_VALUES = [
27
+ '--5',
28
+ 'A5',
29
+ '5A',
30
+ NaN,
31
+ null,
32
+ undefined,
33
+ '',
34
+ ' ',
35
+ 'foo',
36
+ [1],
37
+ {},
38
+ 'NaN',
39
+ 'infinity',
40
+ '--Infinity',
41
+ '++Infinity'
42
+ ];
43
+ describe('isNumberLike', () => {
44
+ it('should return true for number-like values', () => {
45
+ NUMBER_LIKE_VALUES.forEach((value) => {
46
+ expect(isNumberLike(value)).toBe(true);
47
+ });
48
+ });
49
+ it('should return false for non-number-like values', () => {
50
+ NON_NUMBER_LIKE_VALUES.forEach((value) => {
51
+ expect(isNumberLike(value)).toBe(false);
52
+ });
53
+ });
54
+ });
55
+ describe('parseNumber', () => {
56
+ it('should parse numbers', () => {
57
+ NUMBER_LIKE_VALUES.forEach((value) => {
58
+ expect(Number.isNaN(parseNumber(value))).toBe(false);
59
+ });
60
+ });
61
+ it('should return NaN for non-numbers', () => {
62
+ NON_NUMBER_LIKE_VALUES.forEach((value) => {
63
+ expect(Number.isNaN(parseNumber(value))).toBe(true);
64
+ });
65
+ });
66
+ });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@douglasneuroinformatics/libjs",
3
3
  "type": "module",
4
- "version": "0.4.0",
4
+ "version": "0.5.0",
5
5
  "packageManager": "pnpm@9.3.0",
6
6
  "description": "A collection of utility functions and types for Node.js and the browser",
7
7
  "author": "Joshua Unrau",