@ribbon-studios/js-utils 1.1.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.
@@ -0,0 +1,22 @@
1
+ ### 🤖 Contributing to Utils 💗
2
+
3
+ See an [issue](/issues) or need that you're able to fulfill?
4
+ Then this is the perfect place for you!
5
+
6
+ ## Prerequisites
7
+
8
+ - NodeJS 18
9
+
10
+ ## Setting Up Locally
11
+
12
+ - Install the Dependencies
13
+
14
+ ```bash
15
+ bun install
16
+ ```
17
+
18
+ ## Run the Tests~
19
+
20
+ ```bash
21
+ bun run test
22
+ ```
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Rainbow Cafe
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,113 @@
1
+ [![NPM Version][npm-version-image]][npm-url]
2
+ [![NPM Downloads][npm-downloads-image]][npm-url]
3
+ [![Coveralls][coveralls-image]][coveralls-url]
4
+
5
+ [![CI Build][github-actions-image]][github-actions-url]
6
+ [![Maintainability][maintainability-image]][maintainability-url]
7
+ [![Semantic Release][semantic-release-image]][semantic-release-url]
8
+ [![Code Style: Prettier][code-style-image]][code-style-url]
9
+
10
+ </div>
11
+
12
+ # JS Utils 🔧
13
+
14
+ Collection of generic javascript utilities curated by the Rainbow Cafe~
15
+
16
+ - [Promises](#promises)
17
+ - [`delay`](#delay)
18
+ - [`delay.fallback`](#delayfallback)
19
+ - [`assert`](#assert)
20
+ - [`assert.defined`](#assertdefined)
21
+ - [`never`](#never)
22
+
23
+ ## Promises
24
+
25
+ ### `delay`
26
+
27
+ Creates a delayed promise for the given amount of seconds and given promises.
28
+ This can be useful for allowing spinners / loading skeletons to exist for a bit rather then quickly popping in and out.
29
+
30
+ ```tsx
31
+ import { delay } from '@ribbon-studios/js-utils';
32
+
33
+ const promise = delay(); // Returns a promise with the preset delay
34
+ const promise = delay(1000); // Returns a promise with the given delay
35
+ const promise = delay(Promise.resolve('hello')); // Returns the original promise with the preset delay
36
+ const promise = delay(Promise.resolve('hello'), 1000); // Returns the original promise with the given delay
37
+ ```
38
+
39
+ ### `delay.fallback`
40
+
41
+ This overrides the default delay value
42
+
43
+ ```tsx
44
+ import { delay } from '@ribbon-studios/js-utils';
45
+
46
+ const promise = delay(); // Returns a promise with a delay of 500ms
47
+ delay.fallback(100);
48
+ const promise = delay(); // Returns a promise with a delay of 100ms
49
+ ```
50
+
51
+ ### `assert`
52
+
53
+ On its own assert isn't overly useful just because of how type assertion works.
54
+
55
+ ```tsx
56
+ import { assert } from '@ribbon-studios/js-utils';
57
+
58
+ export async function example() {
59
+ const promise: Promise<string | undefined> = Promise.resolve('hello');
60
+ // Run an assertion on the promises response
61
+ const assertedPromise: Promise<string | undefined> = assert(promise, (value) => typeof value !== 'undefined');
62
+ // Unfortunately our response is still considered undefined because we can't forward the assertion
63
+ }
64
+ ```
65
+
66
+ ### `assert.defined`
67
+
68
+ Ensures the promise result is defined.
69
+
70
+ ```tsx
71
+ import { assert } from '@ribbon-studios/js-utils';
72
+
73
+ export async function example() {
74
+ const promise: Promise<string | undefined> = Promise.resolve('hello');
75
+
76
+ // Run an assertion on the promises response
77
+ const assertedPromise: Promise<string> = assert.defined(promise);
78
+ // Our promise is no longer considered undefined!
79
+
80
+ // You can also pass a message to throw!
81
+ const assertedPromise: Promise<string> = assert.defined(promise, 'Expected our thing to exist!');
82
+ }
83
+ ```
84
+
85
+ ### `never`
86
+
87
+ Creates a promise that never resolves.
88
+ Primary use-case for this is testing loading states.
89
+
90
+ In the event a promise is passed it will log a warning in the console as a reminder not to leave it in.
91
+
92
+ ```tsx
93
+ import { never } from '@ribbon-studios/js-utils';
94
+
95
+ const promise = never(); // Returns a promise that never resolves
96
+ const promise = never(Promise.resolve('hello')); // Returns a promise that never resolves
97
+ ```
98
+
99
+ [_**Want to Contribute?**_](/CONTRIBUTING.md)
100
+
101
+ [npm-version-image]: https://img.shields.io/npm/v/@ribbon-studios/js-utils.svg
102
+ [npm-downloads-image]: https://img.shields.io/npm/dm/@ribbon-studios/js-utils.svg
103
+ [npm-url]: https://npmjs.org/package/@ribbon-studios/js-utils
104
+ [github-actions-image]: https://img.shields.io/github/actions/workflow/status/ribbon-studios/js-utils/ci.yml?event=push
105
+ [github-actions-url]: https://github.com/ribbon-studios/js-utils/actions/workflows/ci.yml?query=branch%3Amain
106
+ [coveralls-image]: https://img.shields.io/coveralls/ribbon-studios/js-utils.svg
107
+ [coveralls-url]: https://coveralls.io/github/ribbon-studios/js-utils?branch=main
108
+ [code-style-image]: https://img.shields.io/badge/code%20style-prettier-ff69b4.svg
109
+ [code-style-url]: https://prettier.io
110
+ [maintainability-image]: https://img.shields.io/codeclimate/maintainability/ribbon-studios/refreshly
111
+ [maintainability-url]: https://codeclimate.com/github/ribbon-studios/refreshly/maintainability
112
+ [semantic-release-url]: https://github.com/semantic-release/semantic-release
113
+ [semantic-release-image]: https://img.shields.io/badge/%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079
package/dist/index.cjs ADDED
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ async function assert(p, predicate, message) {
4
+ const value = await p;
5
+ if (predicate(value))
6
+ return value;
7
+ throw new Error(message ?? "Value does not satisfy predicate");
8
+ }
9
+ ((assert2) => {
10
+ async function defined(p, message) {
11
+ return assert2(p, (v) => v !== void 0 && v !== null, message);
12
+ }
13
+ assert2.defined = defined;
14
+ })(assert || (assert = {}));
15
+ let fallbackDelay = 500;
16
+ async function delay(promise, ms) {
17
+ if (typeof promise === "number") {
18
+ return await new Promise((resolve) => setTimeout(resolve, promise));
19
+ }
20
+ const [result] = await Promise.all([promise, new Promise((resolve) => setTimeout(resolve, ms ?? fallbackDelay))]);
21
+ return result;
22
+ }
23
+ ((delay2) => {
24
+ function fallback(delay3) {
25
+ fallbackDelay = delay3;
26
+ }
27
+ delay2.fallback = fallback;
28
+ })(delay || (delay = {}));
29
+ async function never(p) {
30
+ if (p)
31
+ console.warn(`Promise is being called via "never", please ensure this doesn't get deployed!`, p);
32
+ return new Promise(() => {
33
+ });
34
+ }
35
+ exports.assert = assert;
36
+ exports.delay = delay;
37
+ exports.never = never;
@@ -0,0 +1 @@
1
+ export * from './promises';
@@ -0,0 +1 @@
1
+ export * from './promises';
package/dist/index.js ADDED
@@ -0,0 +1,37 @@
1
+ async function assert(p, predicate, message) {
2
+ const value = await p;
3
+ if (predicate(value))
4
+ return value;
5
+ throw new Error(message ?? "Value does not satisfy predicate");
6
+ }
7
+ ((assert2) => {
8
+ async function defined(p, message) {
9
+ return assert2(p, (v) => v !== void 0 && v !== null, message);
10
+ }
11
+ assert2.defined = defined;
12
+ })(assert || (assert = {}));
13
+ let fallbackDelay = 500;
14
+ async function delay(promise, ms) {
15
+ if (typeof promise === "number") {
16
+ return await new Promise((resolve) => setTimeout(resolve, promise));
17
+ }
18
+ const [result] = await Promise.all([promise, new Promise((resolve) => setTimeout(resolve, ms ?? fallbackDelay))]);
19
+ return result;
20
+ }
21
+ ((delay2) => {
22
+ function fallback(delay3) {
23
+ fallbackDelay = delay3;
24
+ }
25
+ delay2.fallback = fallback;
26
+ })(delay || (delay = {}));
27
+ async function never(p) {
28
+ if (p)
29
+ console.warn(`Promise is being called via "never", please ensure this doesn't get deployed!`, p);
30
+ return new Promise(() => {
31
+ });
32
+ }
33
+ export {
34
+ assert,
35
+ delay,
36
+ never
37
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Ensures a promise matches the given predicate
3
+ * @returns The original promise
4
+ */
5
+ export declare function assert<P extends Promise<any>, V = P extends Promise<infer V> ? V : never>(p: P, predicate: (value: V) => boolean, message?: string): Promise<V>;
6
+ export declare namespace assert {
7
+ /**
8
+ * Ensures a promise result is defined
9
+ * @returns The original promise
10
+ */
11
+ function defined<P extends Promise<any>, V = Awaited<P>>(p: P, message?: string): Promise<Exclude<V, undefined | null>>;
12
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * If requests resolve instantly it can lead to users feeling like nothing *really* happened.
3
+ * This just ensures a request always takes a *little* bit of time so that spinners and what not can appear.
4
+ *
5
+ * @param promise The promise to return upon resolving
6
+ * @param ms The time to wait
7
+ * @returns A promise that resolves based on the time specified
8
+ */
9
+ export declare function delay(): Promise<void>;
10
+ export declare function delay(ms: number): Promise<void>;
11
+ export declare function delay<T>(promise: Promise<T>): Promise<T>;
12
+ export declare function delay<T>(promise: Promise<T>, ms: number): Promise<T>;
13
+ export declare namespace delay {
14
+ /**
15
+ * Overrides the default delay value
16
+ */
17
+ function fallback(delay: number): void;
18
+ }
@@ -0,0 +1,3 @@
1
+ export * from './assert';
2
+ export * from './delay';
3
+ export * from './never';
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Purely for testing spinners / skeletons
3
+ * @returns A promise that never resolves
4
+ */
5
+ export declare function never(p?: Promise<any>): Promise<void>;
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@ribbon-studios/js-utils",
3
+ "version": "1.1.1",
4
+ "description": "Collection of generic javascript utilities curated by the Rainbow Cafe~",
5
+ "type": "module",
6
+ "source": "src/*.ts",
7
+ "main": "./dist/index.cjs",
8
+ "module": "./dist/index.module.js",
9
+ "unpkg": "./dist/index.umd.js",
10
+ "types": "dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "import": {
14
+ "types": "./dist/index.d.ts",
15
+ "default": "./dist/index.js"
16
+ },
17
+ "require": {
18
+ "types": "./dist/index.d.cts",
19
+ "default": "./dist/index.cjs"
20
+ }
21
+ }
22
+ },
23
+ "scripts": {
24
+ "ci": "bun install --frozen-lockfile",
25
+ "test": "vitest",
26
+ "test:coverage": "vitest --coverage",
27
+ "lint": "eslint 'src/**/*.ts'",
28
+ "build": "rm -rf dist && vite build"
29
+ },
30
+ "devDependencies": {
31
+ "@types/node": "^20.11.20",
32
+ "@typescript-eslint/eslint-plugin": "^7.0.2",
33
+ "@typescript-eslint/parser": "^7.0.2",
34
+ "@vitest/coverage-v8": "^1.3.1",
35
+ "chance": "^1.1.11",
36
+ "eslint": "^8.57.0",
37
+ "eslint-config-prettier": "^9.1.0",
38
+ "eslint-plugin-unused-imports": "^3.1.0",
39
+ "happy-dom": "^13.5.0",
40
+ "typescript": "^5.3.3",
41
+ "vite": "^5.1.4",
42
+ "vite-plugin-dts": "^3.7.3",
43
+ "vite-plugin-lib-types": "^3.0.9",
44
+ "vitest": "^1.3.1",
45
+ "vitest-dom": "^0.1.1"
46
+ },
47
+ "publishConfig": {
48
+ "access": "public"
49
+ },
50
+ "repository": {
51
+ "type": "git",
52
+ "url": "git+https://github.com/ribbon-studios/js-utils.git"
53
+ },
54
+ "license": "MIT"
55
+ }