@oliversalzburg/js-utils 0.0.2 → 0.0.3
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/README.md +1 -48
- package/output/array.d.ts +28 -1
- package/output/array.js +27 -0
- package/output/array.js.map +1 -1
- package/output/core.d.ts +4 -0
- package/output/core.js +2 -0
- package/output/core.js.map +1 -0
- package/output/nil.d.ts +16 -7
- package/output/nil.js +9 -4
- package/output/nil.js.map +1 -1
- package/output/sleep.d.ts +2 -2
- package/output/sleep.js +3 -3
- package/output/sleep.js.map +1 -1
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -1,48 +1 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
## Does
|
|
4
|
-
|
|
5
|
-
- [Yarn 3](https://yarnpkg.com/)
|
|
6
|
-
- Uses [TypeScript](https://www.typescriptlang.org/)
|
|
7
|
-
- Target application runs in [Node.js](https://nodejs.org/)
|
|
8
|
-
- [esbuild](https://esbuild.github.io/)
|
|
9
|
-
- [EditorConfig](https://editorconfig.org/) + [ESLint](https://eslint.org/) + [Prettier](https://prettier.io/) + [lint-staged](https://github.com/okonet/lint-staged)
|
|
10
|
-
- QA with [GitHub Actions](https://github.com/features/actions)
|
|
11
|
-
- [MkDocs](https://www.mkdocs.org/)-oriented Markdown in mind
|
|
12
|
-
|
|
13
|
-
## Doesn't
|
|
14
|
-
|
|
15
|
-
- [Yarn PnP](https://yarnpkg.com/features/pnp)
|
|
16
|
-
- [Yarn telemetry](https://yarnpkg.com/advanced/telemetry)
|
|
17
|
-
- [Yarn Zero-Installs](https://yarnpkg.com/features/zero-installs)
|
|
18
|
-
- [Husky](https://github.com/typicode/husky)
|
|
19
|
-
|
|
20
|
-
## Why
|
|
21
|
-
|
|
22
|
-
1. Why modern Yarn?
|
|
23
|
-
|
|
24
|
-
Among other things, Yarn is robust, performant and extremely versatile when it comes to repository management. The given configuration provides a very solid first-time experience. Some of the things that make modern Yarn great can be seen in this project template.
|
|
25
|
-
|
|
26
|
-
1. Why esbuild?
|
|
27
|
-
|
|
28
|
-
We want fast builds. The code is still checked for correctness in the editor and in CI.
|
|
29
|
-
|
|
30
|
-
1. Why the MkDocs Markdown?
|
|
31
|
-
|
|
32
|
-
If you're going to want to build any documentation site, you will appreciate having auto-formatted to this style from the start. Using mkdocs-material for documentation sites is highly recommended. The Python Markdown ecosystem is much more versatile and performant than the JS/TS space.
|
|
33
|
-
|
|
34
|
-
1. Why is PnP disabled?
|
|
35
|
-
|
|
36
|
-
While PnP is generally preferred for its strictness, using it still incurs a lot of friction. Because PnP is enabled by default in modern Yarn, this deters many users from adopting modern Yarn. Thus, this project uses Yarn's `node-modules` linker, the behavior of which should be very familiar to most adopters.
|
|
37
|
-
|
|
38
|
-
1. Why is telemetry disabled?
|
|
39
|
-
|
|
40
|
-
Whenever possible, the disclosure of _any_ data should be opt-in. Please refer to the document linked above, and enable telemetry after carefully reading it.
|
|
41
|
-
|
|
42
|
-
1. Why not Zero-Installs?
|
|
43
|
-
|
|
44
|
-
Zero-Installs are purely a `.gitignore`-controlled feature. Most new adopters do not expect the behavior of the default configuration and some adopters have raised concerns over long-term effects. Enable it after carefully reading the article linked above.
|
|
45
|
-
|
|
46
|
-
1. Why not use Husky for the hook?
|
|
47
|
-
|
|
48
|
-
Husky is more complex than what is required to install the hook in a modern Yarn repository. We also do not want to hand over control over repository behavior to a dependency.
|
|
1
|
+
# JS Utils
|
package/output/array.d.ts
CHANGED
|
@@ -1,5 +1,32 @@
|
|
|
1
|
-
import { ConstructorOf } from "./
|
|
1
|
+
import { ConstructorOf } from "./core.js";
|
|
2
|
+
/**
|
|
3
|
+
* Places the items in the array in random order.
|
|
4
|
+
* @param array The array to shuffle.
|
|
5
|
+
* @returns The passed array in random order.
|
|
6
|
+
*/
|
|
2
7
|
export declare const shuffleArray: <T>(array: T[]) => T[];
|
|
8
|
+
/**
|
|
9
|
+
* Returns an array that holds all items that appear in both
|
|
10
|
+
* passed arrays.
|
|
11
|
+
* @param a The first array.
|
|
12
|
+
* @param b The second array.
|
|
13
|
+
* @returns A new array which holds the items that appear in
|
|
14
|
+
* both passed arrays.
|
|
15
|
+
*/
|
|
3
16
|
export declare const intersect: <T>(a: T[], b: T[]) => T[];
|
|
17
|
+
/**
|
|
18
|
+
* Returns an array that holds all items that only appear in `a`.
|
|
19
|
+
* @param a The first array.
|
|
20
|
+
* @param b The second array.
|
|
21
|
+
* @returns A new array which holds the items that ony appear
|
|
22
|
+
* in `a`.
|
|
23
|
+
*/
|
|
4
24
|
export declare const difference: <T>(a: T[], b: T[]) => T[];
|
|
25
|
+
/**
|
|
26
|
+
* From an array with unknown contents, retrieve all the elements
|
|
27
|
+
* that are of a certain type and return them as a new array.
|
|
28
|
+
* @param array The array to filter.
|
|
29
|
+
* @param InstanceType The type to search for.
|
|
30
|
+
* @returns A new array with the filtered items.
|
|
31
|
+
*/
|
|
5
32
|
export declare const filterType: <T>(array: Array<unknown>, InstanceType: ConstructorOf<T>) => T[];
|
package/output/array.js
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import { is } from "./nil.js";
|
|
2
|
+
/**
|
|
3
|
+
* Places the items in the array in random order.
|
|
4
|
+
* @param array The array to shuffle.
|
|
5
|
+
* @returns The passed array in random order.
|
|
6
|
+
*/
|
|
2
7
|
export const shuffleArray = (array) => {
|
|
3
8
|
for (let index = array.length - 1; index > 0; index--) {
|
|
4
9
|
const targetIndex = Math.floor(Math.random() * (index + 1));
|
|
@@ -8,11 +13,33 @@ export const shuffleArray = (array) => {
|
|
|
8
13
|
}
|
|
9
14
|
return array;
|
|
10
15
|
};
|
|
16
|
+
/**
|
|
17
|
+
* Returns an array that holds all items that appear in both
|
|
18
|
+
* passed arrays.
|
|
19
|
+
* @param a The first array.
|
|
20
|
+
* @param b The second array.
|
|
21
|
+
* @returns A new array which holds the items that appear in
|
|
22
|
+
* both passed arrays.
|
|
23
|
+
*/
|
|
11
24
|
export const intersect = (a, b) => {
|
|
12
25
|
return a.filter(x => b.includes(x));
|
|
13
26
|
};
|
|
27
|
+
/**
|
|
28
|
+
* Returns an array that holds all items that only appear in `a`.
|
|
29
|
+
* @param a The first array.
|
|
30
|
+
* @param b The second array.
|
|
31
|
+
* @returns A new array which holds the items that ony appear
|
|
32
|
+
* in `a`.
|
|
33
|
+
*/
|
|
14
34
|
export const difference = (a, b) => {
|
|
15
35
|
return a.filter(x => !b.includes(x));
|
|
16
36
|
};
|
|
37
|
+
/**
|
|
38
|
+
* From an array with unknown contents, retrieve all the elements
|
|
39
|
+
* that are of a certain type and return them as a new array.
|
|
40
|
+
* @param array The array to filter.
|
|
41
|
+
* @param InstanceType The type to search for.
|
|
42
|
+
* @returns A new array with the filtered items.
|
|
43
|
+
*/
|
|
17
44
|
export const filterType = (array, InstanceType) => array.filter(element => is(element, InstanceType));
|
|
18
45
|
//# sourceMappingURL=array.js.map
|
package/output/array.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"array.js","sourceRoot":"","sources":["../source/array.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"array.js","sourceRoot":"","sources":["../source/array.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,EAAE,EAAE,MAAM,UAAU,CAAC;AAE9B;;;;GAIG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAI,KAAe,EAAE,EAAE;IACjD,KAAK,IAAI,KAAK,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,KAAK,GAAG,CAAC,EAAE,KAAK,EAAE,EAAE;QACrD,MAAM,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC;QAC5D,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC;QAC1B,KAAK,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,WAAW,CAAC,CAAC;QAClC,KAAK,CAAC,WAAW,CAAC,GAAG,IAAI,CAAC;KAC3B;IACD,OAAO,KAAK,CAAC;AACf,CAAC,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,CAAI,CAAW,EAAE,CAAW,EAAE,EAAE;IACvD,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;AACtC,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,CAAI,CAAW,EAAE,CAAW,EAAE,EAAE;IACxD,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;AACvC,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,CAAI,KAAqB,EAAE,YAA8B,EAAE,EAAE,CACrF,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE,CAAC,OAAO,EAAE,YAAY,CAAC,CAAa,CAAC"}
|
package/output/core.d.ts
ADDED
package/output/core.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.js","sourceRoot":"","sources":["../source/core.ts"],"names":[],"mappings":""}
|
package/output/nil.d.ts
CHANGED
|
@@ -1,30 +1,39 @@
|
|
|
1
|
+
import { ConstructorOf } from "./core.js";
|
|
2
|
+
/**
|
|
3
|
+
* When you want to normalize away `null` | `undefined`, or deal
|
|
4
|
+
* with either type through a consistent interface.
|
|
5
|
+
*/
|
|
1
6
|
export type Nil = null | undefined;
|
|
2
|
-
export type Maybe<T> = T | Nil;
|
|
3
7
|
/**
|
|
4
|
-
*
|
|
8
|
+
* A type that could be either what you want, or `Nil`.
|
|
5
9
|
*/
|
|
6
|
-
export type
|
|
10
|
+
export type Maybe<T> = T | Nil;
|
|
7
11
|
/**
|
|
8
12
|
* Check if something is a concrete value of the given type.
|
|
9
13
|
* Can be used as a typeguard.
|
|
10
|
-
*
|
|
11
14
|
* @param nilable The subject that could be nil.
|
|
12
15
|
* @param InstanceType The type to check against.
|
|
16
|
+
* @returns `true` if the input element matches the given type,
|
|
17
|
+
* `false` otherwise.
|
|
13
18
|
*/
|
|
14
19
|
export declare function is<T>(nilable: Maybe<T>, InstanceType: ConstructorOf<T>): nilable is T;
|
|
15
20
|
/**
|
|
16
|
-
* Check if something is
|
|
21
|
+
* Check if something is nil.
|
|
17
22
|
* Can be used as a typeguard.
|
|
18
|
-
*
|
|
19
23
|
* @param subject The subject that could be nil.
|
|
24
|
+
* @returns `true` if the subject is nil, `false` otherwise.
|
|
20
25
|
*/
|
|
21
26
|
export declare function isNil<T>(subject: Maybe<T>): subject is Nil;
|
|
27
|
+
/**
|
|
28
|
+
* Thrown when an unexpected nil value was encountered.
|
|
29
|
+
*/
|
|
22
30
|
export declare class UnexpectedNilError extends Error {
|
|
23
31
|
constructor(message?: string);
|
|
24
32
|
}
|
|
25
33
|
/**
|
|
26
34
|
* Ensure that the passed subject is not nil; throw otherwise.
|
|
27
|
-
*
|
|
28
35
|
* @param subject A subject that is possible nil.
|
|
36
|
+
* @returns The subject, if it isn't nil.
|
|
37
|
+
* @throws {UnexpectedNilError} When the subject is nil.
|
|
29
38
|
*/
|
|
30
39
|
export declare function mustExist<T>(subject: Maybe<T>): T;
|
package/output/nil.js
CHANGED
|
@@ -1,22 +1,26 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Check if something is a concrete value of the given type.
|
|
3
3
|
* Can be used as a typeguard.
|
|
4
|
-
*
|
|
5
4
|
* @param nilable The subject that could be nil.
|
|
6
5
|
* @param InstanceType The type to check against.
|
|
6
|
+
* @returns `true` if the input element matches the given type,
|
|
7
|
+
* `false` otherwise.
|
|
7
8
|
*/
|
|
8
9
|
export function is(nilable, InstanceType) {
|
|
9
10
|
return !isNil(nilable) && nilable instanceof InstanceType;
|
|
10
11
|
}
|
|
11
12
|
/**
|
|
12
|
-
* Check if something is
|
|
13
|
+
* Check if something is nil.
|
|
13
14
|
* Can be used as a typeguard.
|
|
14
|
-
*
|
|
15
15
|
* @param subject The subject that could be nil.
|
|
16
|
+
* @returns `true` if the subject is nil, `false` otherwise.
|
|
16
17
|
*/
|
|
17
18
|
export function isNil(subject) {
|
|
18
19
|
return subject === null || subject === undefined;
|
|
19
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Thrown when an unexpected nil value was encountered.
|
|
23
|
+
*/
|
|
20
24
|
export class UnexpectedNilError extends Error {
|
|
21
25
|
constructor(message = "unexpected nil value") {
|
|
22
26
|
super(message);
|
|
@@ -24,8 +28,9 @@ export class UnexpectedNilError extends Error {
|
|
|
24
28
|
}
|
|
25
29
|
/**
|
|
26
30
|
* Ensure that the passed subject is not nil; throw otherwise.
|
|
27
|
-
*
|
|
28
31
|
* @param subject A subject that is possible nil.
|
|
32
|
+
* @returns The subject, if it isn't nil.
|
|
33
|
+
* @throws {UnexpectedNilError} When the subject is nil.
|
|
29
34
|
*/
|
|
30
35
|
export function mustExist(subject) {
|
|
31
36
|
if (isNil(subject)) {
|
package/output/nil.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nil.js","sourceRoot":"","sources":["../source/nil.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"nil.js","sourceRoot":"","sources":["../source/nil.ts"],"names":[],"mappings":"AAaA;;;;;;;GAOG;AACH,MAAM,UAAU,EAAE,CAAI,OAAiB,EAAE,YAA8B;IACrE,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,OAAO,YAAY,YAAY,CAAC;AAC5D,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,KAAK,CAAI,OAAiB;IACxC,OAAO,OAAO,KAAK,IAAI,IAAI,OAAO,KAAK,SAAS,CAAC;AACnD,CAAC;AAED;;GAEG;AACH,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAC3C,YAAY,OAAO,GAAG,sBAAsB;QAC1C,KAAK,CAAC,OAAO,CAAC,CAAC;IACjB,CAAC;CACF;AAED;;;;;GAKG;AACH,MAAM,UAAU,SAAS,CAAI,OAAiB;IAC5C,IAAI,KAAK,CAAC,OAAO,CAAC,EAAE;QAClB,MAAM,IAAI,kBAAkB,EAAE,CAAC;KAChC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
package/output/sleep.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Wait a given period before continuing execution.
|
|
3
|
-
*
|
|
4
3
|
* @param duration How many milliseconds to wait.
|
|
4
|
+
* @returns Nothing
|
|
5
5
|
*/
|
|
6
|
-
export declare
|
|
6
|
+
export declare const sleep: (duration: number) => Promise<void>;
|
package/output/sleep.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Wait a given period before continuing execution.
|
|
3
|
-
*
|
|
4
3
|
* @param duration How many milliseconds to wait.
|
|
4
|
+
* @returns Nothing
|
|
5
5
|
*/
|
|
6
|
-
export
|
|
6
|
+
export const sleep = (duration) => {
|
|
7
7
|
return new Promise(resolve => setTimeout(resolve, duration));
|
|
8
|
-
}
|
|
8
|
+
};
|
|
9
9
|
//# sourceMappingURL=sleep.js.map
|
package/output/sleep.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sleep.js","sourceRoot":"","sources":["../source/sleep.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,
|
|
1
|
+
{"version":3,"file":"sleep.js","sourceRoot":"","sources":["../source/sleep.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,QAAgB,EAAiB,EAAE;IACvD,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC;AAC/D,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
3
|
"name": "@oliversalzburg/js-utils",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.3",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Oliver Salzburg <oliver.salzburg@gmail.com>",
|
|
7
7
|
"type": "module",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"build": "yarn run build:tsc",
|
|
18
18
|
"build:esbuild": "node build.js",
|
|
19
19
|
"build:tsc": "tsc",
|
|
20
|
-
"lint:eslint": "eslint source",
|
|
20
|
+
"lint:eslint": "eslint source ./*js",
|
|
21
21
|
"lint:tsc": "tsc --noEmit",
|
|
22
22
|
"npm:publish": "npm publish",
|
|
23
23
|
"npm:publish:major": "npm version major && npm publish",
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
"@typescript-eslint/parser": "6.9.0",
|
|
34
34
|
"esbuild": "0.19.5",
|
|
35
35
|
"eslint": "8.52.0",
|
|
36
|
+
"eslint-plugin-jsdoc": "46.8.2",
|
|
36
37
|
"lint-staged": "15.0.2",
|
|
37
38
|
"prettier": "3.0.3",
|
|
38
39
|
"prettier-package-json": "2.8.0",
|