@oliversalzburg/js-utils 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 +21 -0
- package/README.md +48 -0
- package/output/array.d.ts +5 -0
- package/output/array.js +18 -0
- package/output/array.js.map +1 -0
- package/output/index.d.ts +3 -0
- package/output/index.js +4 -0
- package/output/index.js.map +1 -0
- package/output/nil.d.ts +30 -0
- package/output/nil.js +36 -0
- package/output/nil.js.map +1 -0
- package/output/sleep.d.ts +6 -0
- package/output/sleep.js +9 -0
- package/output/sleep.js.map +1 -0
- package/package.json +31 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Oliver Salzburg
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# Project Template
|
|
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.
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { ConstructorOf } from "./nil.js";
|
|
2
|
+
export declare const shuffleArray: <T>(array: T[]) => T[];
|
|
3
|
+
export declare const intersect: <T>(a: T[], b: T[]) => T[];
|
|
4
|
+
export declare const difference: <T>(a: T[], b: T[]) => T[];
|
|
5
|
+
export declare const filterType: <T>(array: Array<unknown>, InstanceType: ConstructorOf<T>) => T[];
|
package/output/array.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { is } from "./nil.js";
|
|
2
|
+
export const shuffleArray = (array) => {
|
|
3
|
+
for (let index = array.length - 1; index > 0; index--) {
|
|
4
|
+
const targetIndex = Math.floor(Math.random() * (index + 1));
|
|
5
|
+
const temp = array[index];
|
|
6
|
+
array[index] = array[targetIndex];
|
|
7
|
+
array[targetIndex] = temp;
|
|
8
|
+
}
|
|
9
|
+
return array;
|
|
10
|
+
};
|
|
11
|
+
export const intersect = (a, b) => {
|
|
12
|
+
return a.filter(x => b.includes(x));
|
|
13
|
+
};
|
|
14
|
+
export const difference = (a, b) => {
|
|
15
|
+
return a.filter(x => !b.includes(x));
|
|
16
|
+
};
|
|
17
|
+
export const filterType = (array, InstanceType) => array.filter(element => is(element, InstanceType));
|
|
18
|
+
//# sourceMappingURL=array.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"array.js","sourceRoot":"","sources":["../source/array.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,EAAE,EAAE,MAAM,UAAU,CAAC;AAE7C,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,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,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,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/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../source/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC"}
|
package/output/nil.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export type Nil = null | undefined;
|
|
2
|
+
export type Maybe<T> = T | Nil;
|
|
3
|
+
/**
|
|
4
|
+
* Describes a function that is a constructor for T.
|
|
5
|
+
*/
|
|
6
|
+
export type ConstructorOf<T = Record<string, unknown>> = new (...args: any[]) => T;
|
|
7
|
+
/**
|
|
8
|
+
* Check if something is a concrete value of the given type.
|
|
9
|
+
* Can be used as a typeguard.
|
|
10
|
+
*
|
|
11
|
+
* @param nilable The subject that could be nil.
|
|
12
|
+
* @param InstanceType The type to check against.
|
|
13
|
+
*/
|
|
14
|
+
export declare function is<T>(nilable: Maybe<T>, InstanceType: ConstructorOf<T>): nilable is T;
|
|
15
|
+
/**
|
|
16
|
+
* Check if something is Nil.
|
|
17
|
+
* Can be used as a typeguard.
|
|
18
|
+
*
|
|
19
|
+
* @param subject The subject that could be nil.
|
|
20
|
+
*/
|
|
21
|
+
export declare function isNil<T>(subject: Maybe<T>): subject is Nil;
|
|
22
|
+
export declare class UnexpectedNilError extends Error {
|
|
23
|
+
constructor(message?: string);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Ensure that the passed subject is not nil; throw otherwise.
|
|
27
|
+
*
|
|
28
|
+
* @param subject A subject that is possible nil.
|
|
29
|
+
*/
|
|
30
|
+
export declare function mustExist<T>(subject: Maybe<T>): T;
|
package/output/nil.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check if something is a concrete value of the given type.
|
|
3
|
+
* Can be used as a typeguard.
|
|
4
|
+
*
|
|
5
|
+
* @param nilable The subject that could be nil.
|
|
6
|
+
* @param InstanceType The type to check against.
|
|
7
|
+
*/
|
|
8
|
+
export function is(nilable, InstanceType) {
|
|
9
|
+
return !isNil(nilable) && nilable instanceof InstanceType;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Check if something is Nil.
|
|
13
|
+
* Can be used as a typeguard.
|
|
14
|
+
*
|
|
15
|
+
* @param subject The subject that could be nil.
|
|
16
|
+
*/
|
|
17
|
+
export function isNil(subject) {
|
|
18
|
+
return subject === null || subject === undefined;
|
|
19
|
+
}
|
|
20
|
+
export class UnexpectedNilError extends Error {
|
|
21
|
+
constructor(message = "unexpected nil value") {
|
|
22
|
+
super(message);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Ensure that the passed subject is not nil; throw otherwise.
|
|
27
|
+
*
|
|
28
|
+
* @param subject A subject that is possible nil.
|
|
29
|
+
*/
|
|
30
|
+
export function mustExist(subject) {
|
|
31
|
+
if (isNil(subject)) {
|
|
32
|
+
throw new UnexpectedNilError();
|
|
33
|
+
}
|
|
34
|
+
return subject;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=nil.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nil.js","sourceRoot":"","sources":["../source/nil.ts"],"names":[],"mappings":"AAQA;;;;;;GAMG;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,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAC3C,YAAY,OAAO,GAAG,sBAAsB;QAC1C,KAAK,CAAC,OAAO,CAAC,CAAC;IACjB,CAAC;CACF;AAED;;;;GAIG;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.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sleep.js","sourceRoot":"","sources":["../source/sleep.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,UAAU,KAAK,CAAC,QAAgB;IACpC,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC;AAC/D,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@oliversalzburg/js-utils",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"author": "Oliver Salzburg <oliver.salzburg@gmail.com>",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": "./output/index.js",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "yarn run build:tsc",
|
|
10
|
+
"build:esbuild": "node build.js",
|
|
11
|
+
"build:tsc": "tsc",
|
|
12
|
+
"lint:eslint": "eslint source",
|
|
13
|
+
"lint:tsc": "tsc --noEmit",
|
|
14
|
+
"prepack": "yarn run build"
|
|
15
|
+
},
|
|
16
|
+
"types": "./output/index.d.ts",
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@types/eslint": "8.44.6",
|
|
19
|
+
"@types/node": "20.8.9",
|
|
20
|
+
"@typescript-eslint/eslint-plugin": "6.9.0",
|
|
21
|
+
"@typescript-eslint/parser": "6.9.0",
|
|
22
|
+
"esbuild": "0.19.5",
|
|
23
|
+
"eslint": "8.52.0",
|
|
24
|
+
"lint-staged": "15.0.2",
|
|
25
|
+
"prettier": "3.0.3",
|
|
26
|
+
"prettier-package-json": "2.8.0",
|
|
27
|
+
"prettier-plugin-organize-imports": "3.2.3",
|
|
28
|
+
"typescript": "5.2.2"
|
|
29
|
+
},
|
|
30
|
+
"packageManager": "yarn@4.0.0"
|
|
31
|
+
}
|