Skip to content

Commit

Permalink
Minor fixes and a helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ogroppo committed Jul 17, 2024
1 parent ad5d2ff commit b824d39
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 16 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# deverything

## 1.1.0

### Minor Changes

- minor fixes and more helpers

## 1.0.0

### Major Changes
Expand All @@ -13,6 +19,7 @@
- randomMaxDate
- checkEnvVars is dropped
- randomPercentage is dropped
- randomPositivePercentage is dropped

## 0.51.1

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ Contributions always welcome!
- `arrayDiff()` get the difference of two arrays
- `arrayIntersection()` get the intersection of two arrays
- `capitalize()` word => Word
- `cleanSpaces()` trims and turns double spaces into single space
- `clamp()` clamp number in a range
- `cleanSpaces()` trims and turns double spaces into single space
- `enumKeys()` enum FRUIT { APPLE, PEAR } => ["APPLE", "PEAR"]
- `enumValues()` enum FRUIT { APPLE = 1, PEAR = 3 } => [1, 3]
- `filterAlphanumeric()` remove non-alphanumeric characters
- `first()` get the first element of an array
- `firstKey()` get the first key of an object
- `firstValue()` get the first value of an object
Expand Down Expand Up @@ -116,6 +117,7 @@ Contributions always welcome!
### Formatters

- `formatCamelCase()`
- `formatCookies()` { cookie1: "1", cookie2: "2" } => "cookie1=1; cookie2=2"
- `formatNumber()` 1000 => "1,000" or "1K" or 0.112 => "11.2%"
- `formatPercentage()` 0.11 => "11%"
- `formatProgress()` => "[2/10]"
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "deverything",
"version": "1.0.0",
"version": "1.1.0",
"description": "Everything you need for Dev",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down Expand Up @@ -54,5 +54,6 @@
"ts-node": "^10.9.2",
"tsup": "^6.7.0",
"typescript": "^4.9.5"
}
},
"packageManager": "[email protected]+sha512.f549b8a52c9d2b8536762f99c0722205efc5af913e77835dbccc3b0b0b2ca9e7dc8022b78062c17291c48e88749c70ce88eb5a74f1fa8c4bf5e18bb46c8bd83a"
}
12 changes: 12 additions & 0 deletions src/formatters/formatCookies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { PlainObject } from "../types/Object";

/**
*
* @example formatCookies({}) => ""
* @example formatCookies({ session: "123", _ga: 123 }) => "session=123; _ga=123"
*/
export const formatCookies = (object: PlainObject): string => {
return Object.entries(object)
.map(([key, value]) => `${key}=${value}`)
.join("; ");
};
1 change: 1 addition & 0 deletions src/formatters/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./formatCamelCase";
export * from "./formatCookies";
export * from "./formatNumber";
export * from "./formatPercentage";
export * from "./formatProgress";
Expand Down
7 changes: 7 additions & 0 deletions src/helpers/filterAlphanumeric.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @returns a string with only alphanumeric characters
* @example filterAlphanumeric("!abc()") // returns "abc"
*/
export const filterAlphanumeric = (string: string) => {
return string.replace(/[^a-zA-Z0-9]/g, "");
};
1 change: 1 addition & 0 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export * from "./cyclicalItem";
export * from "./dir";
export * from "./enumKeys";
export * from "./enumValues";
export * from "./filterAlphanumeric";
export * from "./first";
export * from "./firstKey";
export * from "./firstValue";
Expand Down
41 changes: 35 additions & 6 deletions src/helpers/seriesAll.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,41 @@ 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]);
expect(
await seriesAll<number>([
Promise.resolve(1),
sleep(1).then(() => 2),
() => Promise.resolve(3),
() => 4,
async () => 5,
async () => {
await sleep(1);
return 6;
},
async () => {
return sleep(1).then(() => 7);
},
])
).toStrictEqual([1, 2, 3, 4, 5, 6, 7]);
});

test("throw new Error", () => {
expect(
seriesAll([
() => {
throw new Error("1");
},
() => {
throw new Error("2");
},
])
).rejects.toThrowError("1");
});

test("Promise.reject", () => {
expect(
seriesAll([Promise.reject("3"), () => Promise.reject("4")])
).rejects.toEqual("3");
});
});
18 changes: 13 additions & 5 deletions src/helpers/seriesAll.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import { isFunction, isPromise } from "../validators";

/**
*
* @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(1),
* sleep(100).then(() => 2),
* () => Promise.resolve(3),
* ]); => [1, 2, 3]
* async () => 4,
* ]); => [1, 2, 3, 4]
*/
export const seriesAll = async <T>(series: Function[]): Promise<T[]> => {
export const seriesAll = async <T>(
series: (Promise<T> | Function)[]
): Promise<T[]> => {
const results: T[] = [];
for (const fn of series) {
results.push(await fn());
if (isPromise(fn)) results.push(await fn);

Check failure on line 19 in src/helpers/seriesAll.ts

View workflow job for this annotation

GitHub Actions / version

Argument of type 'Function | Awaited<T>' is not assignable to parameter of type 'T'.
else if (isFunction(fn)) results.push(await fn());
// pure value? just return it
else results.push(fn);

Check failure on line 22 in src/helpers/seriesAll.ts

View workflow job for this annotation

GitHub Actions / version

Argument of type 'Promise<T>' is not assignable to parameter of type 'T'.
}
return results;
};
Expand Down
11 changes: 11 additions & 0 deletions src/types/Date.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
export type DateLike = Date | string | number;
export type Datey = Date | string;

/**
* @example "2021-01-01T00:00:00.000Z"
*/
export type ISODate = string;
/**
* @example "2021-01-01"
*/
export type ISODay = string;
/**
* @example "America/New_York"
*/
export type Timezone = string;

export type DateRange = {
startDate: DateLike;
Expand Down
9 changes: 7 additions & 2 deletions src/validators/isFunction.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
export const isFunction = (arg: any): arg is Function =>
Object.prototype.toString.call(arg) === "[object Function]";
/**
* @returns true if the argument can be called like a function -> fn() or await fn()
*/
export const isFunction = (arg: any): arg is Function => {
const type = Object.prototype.toString.call(arg);
return type === "[object Function]" || type === "[object AsyncFunction]";
};

0 comments on commit b824d39

Please sign in to comment.