@douglasneuroinformatics/libjs 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/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
package/README.md ADDED
@@ -0,0 +1,49 @@
1
+ <!-- PROJECT LOGO -->
2
+ <div align="center">
3
+ <a href="https://github.com/DouglasNeuroInformatics/libjs">
4
+ <img src="https://raw.githubusercontent.com/DouglasNeuroInformatics/libjs/main/.github/assets/libjs-logo.png" alt="Logo" width="100" >
5
+ </a>
6
+ <h3 align="center">@douglasneuroinformatics/libjs</h3>
7
+ <p align="center">
8
+ A collection of utility functions and types for Node.js and the browser
9
+ <br />
10
+ <a href="https://douglasneuroinformatics.github.io/libjs/">
11
+ <strong>Explore the docs »
12
+ </strong>
13
+ </a>
14
+ <br />
15
+ <br />
16
+ <a href="https://github.com/DouglasNeuroInformatics/libjs/issues" rel="noreferrer" target="_blank">Report Bug</a>
17
+ ·
18
+ <a href="https://github.com/DouglasNeuroInformatics/libjs/issues" rel="noreferrer" target="_blank">Request Feature</a>
19
+ </p>
20
+ </div>
21
+
22
+ <!-- PROJECT SHIELDS -->
23
+ <div align="center">
24
+
25
+ ![license](https://img.shields.io/github/license/DouglasNeuroInformatics/libjs)
26
+ ![version](https://img.shields.io/github/package-json/v/DouglasNeuroInformatics/libjs)
27
+ [![codecov](https://codecov.io/gh/DouglasNeuroInformatics/libjs/graph/badge.svg?token=J9XY9jkGs1)](https://codecov.io/gh/DouglasNeuroInformatics/libjs)
28
+
29
+ </div>
30
+ <hr />
31
+
32
+ ## Installation
33
+
34
+ ```sh
35
+ pnpm install @douglasneuroinformatics/libjs
36
+ ```
37
+
38
+ ## Contributing
39
+
40
+ We welcome contributions! If you're interested in improving the library or adding new features, please refer to our contribution guide.
41
+
42
+ ## License
43
+
44
+ Copyright (C) 2024 Douglas Neuroinformatics Platform
45
+
46
+ This program is free software: you can redistribute it and/or modify
47
+ it under the terms of the GNU Lesser General Public License as published by
48
+ the Free Software Foundation, either version 3 of the License, or
49
+ (at your option) any later version.
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Returns the data in basic ISO format, e.g., yyyy-mm-dd
3
+ * @example
4
+ * ```ts
5
+ * // returns '2000-01-01'
6
+ * toBasicISOString(new Date(2000, 0, 1))
7
+ * ```
8
+ */
9
+ export declare function toBasicISOString(date: Date): string;
10
+ /**
11
+ * Returns the number of years since `date`, rounded down
12
+ * @example
13
+ * ```ts
14
+ * const today = new Date();
15
+ * const date = new Date(today.setMonth(today.getMonth() - 18));
16
+ * yearsPassed(date); // 1
17
+ * ```
18
+ */
19
+ export declare function yearsPassed(date: Date): number;
20
+ /**
21
+ * Pause the flow of execution in an async function
22
+ * * @example
23
+ * ```ts
24
+ * await sleep(5);
25
+ * ```
26
+ */
27
+ export declare function sleep(seconds: number): Promise<unknown>;
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Returns the data in basic ISO format, e.g., yyyy-mm-dd
3
+ * @example
4
+ * ```ts
5
+ * // returns '2000-01-01'
6
+ * toBasicISOString(new Date(2000, 0, 1))
7
+ * ```
8
+ */
9
+ export function toBasicISOString(date) {
10
+ return date.toISOString().split('T')[0];
11
+ }
12
+ /**
13
+ * Returns the number of years since `date`, rounded down
14
+ * @example
15
+ * ```ts
16
+ * const today = new Date();
17
+ * const date = new Date(today.setMonth(today.getMonth() - 18));
18
+ * yearsPassed(date); // 1
19
+ * ```
20
+ */
21
+ export function yearsPassed(date) {
22
+ return new Date(Date.now() - date.getTime()).getFullYear() - 1970;
23
+ }
24
+ /**
25
+ * Pause the flow of execution in an async function
26
+ * * @example
27
+ * ```ts
28
+ * await sleep(5);
29
+ * ```
30
+ */
31
+ export async function sleep(seconds) {
32
+ return new Promise((resolve) => setTimeout(resolve, seconds / 1000));
33
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,19 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { toBasicISOString, yearsPassed } from './datetime.js';
3
+ describe('toBasicISOString', () => {
4
+ it('should return a string of the format yyyy-mm-dd', () => {
5
+ expect(toBasicISOString(new Date(2000, 0, 1))).toBe('2000-01-01');
6
+ });
7
+ });
8
+ describe('yearsPassed', () => {
9
+ it('should return zero for the date eleven months ago', () => {
10
+ const today = new Date();
11
+ const date = new Date(today.setMonth(today.getMonth() - 11));
12
+ expect(yearsPassed(date)).toBe(0);
13
+ });
14
+ it('should return one for the date eighteen months ago', () => {
15
+ const today = new Date();
16
+ const date = new Date(today.setMonth(today.getMonth() - 18));
17
+ expect(yearsPassed(date)).toBe(1);
18
+ });
19
+ });
@@ -0,0 +1,5 @@
1
+ export * from './datetime.js';
2
+ export * from './object.js';
3
+ export * from './random.js';
4
+ export * from './range.js';
5
+ export * from './string.js';
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export * from './datetime.js';
2
+ export * from './object.js';
3
+ export * from './random.js';
4
+ export * from './range.js';
5
+ export * from './string.js';
@@ -0,0 +1,4 @@
1
+ export type ReadonlyDeep<T extends object> = Readonly<{
2
+ [K in keyof T]: T[K] extends object ? ReadonlyDeep<T[K]> : T[K];
3
+ }>;
4
+ export declare function deepFreeze<T extends object>(obj: T): ReadonlyDeep<T>;
package/dist/object.js ADDED
@@ -0,0 +1,9 @@
1
+ export function deepFreeze(obj) {
2
+ Object.keys(obj).forEach((key) => {
3
+ const value = obj[key];
4
+ if (value && typeof value === 'object') {
5
+ deepFreeze(value);
6
+ }
7
+ });
8
+ return Object.freeze(obj);
9
+ }
@@ -0,0 +1,6 @@
1
+ /** Returns a random integer between `min` (inclusive) and `max` (not inclusive) */
2
+ export declare function randomInt(min: number, max: number): number;
3
+ /** Returns a random date between `start` and `end` (both inclusive) */
4
+ export declare function randomDate(start: Date, end: Date): Date;
5
+ /** Returns a random value from the array */
6
+ export declare function randomValue<T>(arr: T[]): T;
package/dist/random.js ADDED
@@ -0,0 +1,21 @@
1
+ /** Returns a random integer between `min` (inclusive) and `max` (not inclusive) */
2
+ export function randomInt(min, max) {
3
+ if (min >= max) {
4
+ throw new Error(`Min value '${min}' must not be greater than or equal to the max value '${max}'`);
5
+ }
6
+ return Math.floor(Math.random() * (max - min)) + min;
7
+ }
8
+ /** Returns a random date between `start` and `end` (both inclusive) */
9
+ export function randomDate(start, end) {
10
+ if (start > end) {
11
+ throw new Error(`Start date '${start.toISOString()}' cannot be greater than end date '${end.toISOString()}'`);
12
+ }
13
+ return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
14
+ }
15
+ /** Returns a random value from the array */
16
+ export function randomValue(arr) {
17
+ if (arr.length === 0) {
18
+ throw new Error('Cannot select random value from array of length zero');
19
+ }
20
+ return arr[randomInt(0, arr.length)];
21
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,39 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { randomDate, randomInt } from './random.js';
3
+ describe('randomInt', () => {
4
+ it('should return an integer value within the range', () => {
5
+ const min = 5;
6
+ const max = 8;
7
+ const result = randomInt(min, max);
8
+ expect(result).toBeGreaterThanOrEqual(min);
9
+ expect(result).toBeLessThan(8);
10
+ expect(Number.isInteger(result)).toBe(true);
11
+ });
12
+ it('should throw if the min value is larger than the max', () => {
13
+ expect(() => randomInt(10, 5)).toThrow();
14
+ });
15
+ it('should throw if the min value equals the max', () => {
16
+ expect(() => randomInt(10, 10)).toThrow();
17
+ });
18
+ it('should handle negative values', () => {
19
+ const min = -5;
20
+ const max = -3;
21
+ const result = randomInt(min, max);
22
+ expect(result).toBeGreaterThanOrEqual(min);
23
+ expect(result).toBeLessThan(8);
24
+ expect(Number.isInteger(result)).toBe(true);
25
+ expect(() => randomInt(max, min)).toThrow();
26
+ });
27
+ });
28
+ describe('randomDate', () => {
29
+ it('should return a date within the range', () => {
30
+ const start = new Date(2000, 0, 1);
31
+ const end = new Date();
32
+ const random = randomDate(start, end);
33
+ expect(random.getTime() >= start.getTime()).toBe(true);
34
+ expect(random.getTime() <= end.getTime()).toBe(true);
35
+ });
36
+ it('should throw if the end is before the start', () => {
37
+ expect(() => randomDate(new Date(), new Date(2000, 0, 1))).toThrow();
38
+ });
39
+ });
@@ -0,0 +1,4 @@
1
+ /** Return an array of integers between 0 (inclusive) and `end` (not inclusive) */
2
+ export declare function range(end: number): readonly number[];
3
+ /** Return an array of integers between `start` (inclusive) and `end` (not inclusive) */
4
+ export declare function range(start: number, end: number): readonly number[];
package/dist/range.js ADDED
@@ -0,0 +1,13 @@
1
+ /* eslint-disable @typescript-eslint/unified-signatures */
2
+ export function range(...args) {
3
+ const start = args.length === 2 ? args[0] : 0;
4
+ const end = args.length === 2 ? args[1] : args[0];
5
+ if (start >= end) {
6
+ throw new Error(`End of range '${end}' must be greater than start '${start}'`);
7
+ }
8
+ const values = [];
9
+ for (let i = start; i < end; i++) {
10
+ values.push(i);
11
+ }
12
+ return values;
13
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { range } from './range.js';
3
+ describe('range', () => {
4
+ it('should return an array equal in length to the range', () => {
5
+ const arr = range(10);
6
+ expect(arr.length).toBe(10);
7
+ });
8
+ });
@@ -0,0 +1,3 @@
1
+ import type { CamelCase, SnakeCase } from 'type-fest';
2
+ export declare function camelToSnakeCase<T extends string>(s: T): SnakeCase<T>;
3
+ export declare function snakeToCamelCase<T extends string>(s: T): CamelCase<T>;
package/dist/string.js ADDED
@@ -0,0 +1,8 @@
1
+ export function camelToSnakeCase(s) {
2
+ return s.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
3
+ }
4
+ export function snakeToCamelCase(s) {
5
+ return s
6
+ .toLowerCase()
7
+ .replace(/([-_][a-z])/g, (group) => group.toUpperCase().replace('-', '').replace('_', ''));
8
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,16 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { camelToSnakeCase, snakeToCamelCase } from './string.js';
3
+ describe('camelToSnakeCase', () => {
4
+ it('should convert from camel to snake case ', () => {
5
+ expect(camelToSnakeCase('toSnakeCase')).toBe('to_snake_case');
6
+ expect(camelToSnakeCase('foo')).toBe('foo');
7
+ expect(camelToSnakeCase('')).toBe('');
8
+ });
9
+ });
10
+ describe('snakeToCamelCase', () => {
11
+ it('should convert from snake to camel case ', () => {
12
+ expect(snakeToCamelCase('to_camel_case')).toBe('toCamelCase');
13
+ expect(camelToSnakeCase('foo')).toBe('foo');
14
+ expect(camelToSnakeCase('')).toBe('');
15
+ });
16
+ });
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@douglasneuroinformatics/libjs",
3
+ "type": "module",
4
+ "version": "0.0.1",
5
+ "packageManager": "pnpm@8.15.3",
6
+ "description": "A collection of utility functions and types for Node.js and the browser",
7
+ "license": "LGPL-3.0",
8
+ "homepage": "https://github.com/DouglasNeuroInformatics/libjs/#readme",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/DouglasNeuroInformatics/libjs/.git"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/DouglasNeuroInformatics/libjs/issues"
15
+ },
16
+ "main": "./dist/index.js",
17
+ "types": "./dist/index.d.ts",
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "peerDependencies": {
22
+ "typescript": "^5.1.0"
23
+ },
24
+ "dependencies": {
25
+ "type-fest": "^4.11.1"
26
+ },
27
+ "devDependencies": {
28
+ "@douglasneuroinformatics/eslint-config": "^4.0.0",
29
+ "@vitest/coverage-v8": "^1.3.1",
30
+ "eslint": "^8.57.0",
31
+ "prettier": "^3.2.5",
32
+ "typedoc": "^0.25.2",
33
+ "typescript": "~5.3.3",
34
+ "vitest": "^1.3.1"
35
+ },
36
+ "scripts": {
37
+ "build": "tsc",
38
+ "build:docs": "typedoc",
39
+ "format": "prettier --write src",
40
+ "lint": "tsc --noEmit && eslint --fix src",
41
+ "test": "vitest run",
42
+ "test:coverage": "vitest run --coverage"
43
+ }
44
+ }