Skip to content

Commit

Permalink
seriesAll and handle suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
ogroppo committed Jan 5, 2024
1 parent 6c57e4c commit 6df4c9a
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# deverything

## 0.39.0

### Minor Changes

- seriesAll and handle suffix

## 0.38.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "deverything",
"version": "0.38.0",
"version": "0.39.0",
"description": "Everything you need for Dev",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
1 change: 1 addition & 0 deletions src/helpers/clamp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { clamp } from "./clamp";

describe("clamp", () => {
test("args", async () => {
expect(clamp({ number: 2, min: 1, max: 3 })).toBe(2);
expect(clamp({ number: 0, min: 0, max: 0 })).toBe(0);
expect(clamp({ number: -10, min: 0, max: 0 })).toBe(0);
expect(clamp({ number: 10, min: 0, max: 0 })).toBe(0);
Expand Down
1 change: 1 addition & 0 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export * from "./pretty";
export * from "./promiseWithTimeout";
export * from "./scrambleText";
export * from "./serialize";
export * from "./seriesAll";
export * from "./setUrlSearchParams";
export * from "./shuffle";
export * from "./sleep";
Expand Down
14 changes: 14 additions & 0 deletions src/helpers/seriesAll.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, expect, test } from "@jest/globals";
import { seriesAll } from "./seriesAll";
import { sleep } from "./sleep";

describe("seriesAll", () => {
const fn1 = () => Promise.resolve(1);
const fn2 = () => sleep(100).then(() => 2);
const fn3 = () => 3;

test("simple", async () => {
expect(seriesAll<number>([fn1, fn2, fn3])).resolves.toEqual([1, 2, 3]);
expect(await seriesAll([() => true])).toStrictEqual([true]);
});
});
18 changes: 18 additions & 0 deletions src/helpers/seriesAll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
*
* @description Run a series of (async) functions in order and return the results
* @example
* const results = await seriesAll([
* () => Promise.resolve(1),
* () => sleep(100).then(() => 2),
* () => Promise.resolve(3),
* ]);
* @returns [1, 2, 3]
*/
export const seriesAll = async <T>(series: Function[]): Promise<T[]> => {
const results = [];
for (const fn of series) {
results.push(await fn());
}
return results;
};
1 change: 1 addition & 0 deletions src/random/randomEmail.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ describe(`randomEmail`, () => {
expect(randomEmail().length).toBeGreaterThan(0);
expect(randomEmail()).toContain(".");
expect(randomEmail()).toContain("@");
expect(randomEmail({ handleSuffix: "_project" })).toContain("_project");
});
});
9 changes: 7 additions & 2 deletions src/random/randomEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@ import { DOMAIN_NAMES } from "../constants/domains";
import { randomArrayItem } from "./randomArrayItem";
import { randomHandle } from "./randomHandle";

export const randomEmail = () =>
`${randomHandle()}@${randomArrayItem(DOMAIN_NAMES)}`;
export const randomEmail = ({
handle,
handleSuffix,
}: { handle?: string; handleSuffix?: string } = {}) =>
`${handle || randomHandle({ suffix: handleSuffix })}@${randomArrayItem(
DOMAIN_NAMES
)}`;
15 changes: 12 additions & 3 deletions src/random/randomHandle.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { WESTERN_FIRST_NAMES, WESTERN_LAST_NAMES } from "../constants/names";
import { incrementalId } from "../helpers/incrementalId";
import { randomArrayItem } from "./randomArrayItem";
import { randomNumericId } from "./randomNumericId";

export const randomHandle = () =>
/**
*
* @returns a unique social-like handle
* @example "john.doe15"
*/
export const randomHandle = ({ suffix }: { suffix?: string } = {}) =>
(
randomArrayItem(WESTERN_FIRST_NAMES) +
"." +
randomArrayItem(WESTERN_LAST_NAMES)
).toLowerCase() + randomNumericId(); // use randomNumericId too keep handles process-unique
).toLowerCase() +
incrementalId() + // process-unique
suffix
? suffix
: "";
27 changes: 27 additions & 0 deletions src/types/Tuple.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
k: infer I
) => void
? I
: never;

type LastOf<T> = UnionToIntersection<
T extends any ? () => T : never
> extends () => infer R
? R
: never;

type Push<T extends any[], V> = [...T, V];

type TuplifyUnion<
T,
L = LastOf<T>,
N = [T] extends [never] ? true : false
> = true extends N ? [] : Push<TuplifyUnion<Exclude<T, L>>, L>;

// The magic happens here!
export type Tuple<
T,
A extends T[] = []
> = TuplifyUnion<T>["length"] extends A["length"]
? [...A]
: Tuple<T, [T, ...A]>;

0 comments on commit 6df4c9a

Please sign in to comment.