Skip to content

Commit 12ef235

Browse files
committed
More helpers
1 parent 5399983 commit 12ef235

13 files changed

Lines changed: 170 additions & 16 deletions

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# deverything
22

3+
## 5.3.0
4+
5+
### Minor Changes
6+
7+
- deprecated chunkedAsync
8+
- deplecated chunkedDynamic
9+
- added getCountryName
10+
- added getFlagEmoji
11+
- added paginationToSkipTake
12+
- added skipTakeToPagination
13+
314
## 5.2.0
415

516
### Minor Changes

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "deverything",
3-
"version": "5.2.0",
3+
"version": "5.3.0",
44
"description": "Everything you need for Dev",
55
"main": "./dist/index.js",
66
"module": "./dist/index.mjs",

src/helpers/chunkedAsync.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@ import { chunkArray } from "./chunkArray";
22
import { sleep } from "./sleep";
33

44
/**
5-
* @description Run a series of (async) functions in order and return the results
6-
* @param array
7-
* @param chunkSize
8-
* @param fn
9-
* @param options.minChunkTimeMs - Minimum time to process each chunk
5+
* @deprecated Use pMap/pAll/pReduce instead
106
*/
117
export const chunkedAsync = async <T>(
128
array: T[],

src/helpers/chunkedDynamic.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,7 @@ import { clamp } from "./clamp";
22
import { sleep } from "./sleep";
33

44
/**
5-
* @description Run a series of (async) functions with dynamic chunk sizes
6-
* @param array - Array to chunk
7-
* @param initialChunkSize - Size of initial chunk
8-
* @param fn - Function to run on each chunk
9-
* @param options.idealChunkDuration - Ideal time to process each chunk, the chunk size will adjust to meet this duration
10-
* @param options.maxChunkSize - Maximum chunk size (default 200)
11-
* @param options.minChunkDuration - Minimum time to process each chunk (useful for rate limiting)
12-
* @param options.minChunkSize - Minimum chunk size (default 1)
13-
* @param options.sleepTimeMs - Time to sleep between each chunk
5+
* @deprecated Use pMap/pAll/pReduce instead
146
*/
157
export const chunkedDynamic = async <T>(
168
array: T[],

src/helpers/getCountryName.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { describe, expect, test } from "vitest";
2+
import { getCountryName } from "./getCountryName";
3+
4+
describe("getCountryName", () => {
5+
test("iso2 codes", () => {
6+
expect(getCountryName("GB")).toBe("United Kingdom");
7+
expect(getCountryName("US")).toBe("United States");
8+
expect(getCountryName("FR")).toBe("France");
9+
});
10+
11+
test("iso3 codes", () => {
12+
expect(getCountryName("GBR")).toBe("United Kingdom");
13+
expect(getCountryName("USA")).toBe("United States");
14+
expect(getCountryName("FRA")).toBe("France");
15+
});
16+
17+
test("case insensitive", () => {
18+
expect(getCountryName("gb")).toBe("United Kingdom");
19+
expect(getCountryName("gbr")).toBe("United Kingdom");
20+
});
21+
22+
test("falls back to input for unknown codes", () => {
23+
expect(getCountryName("XX")).toBe("XX");
24+
expect(getCountryName("XXX")).toBe("XXX");
25+
});
26+
});

src/helpers/getCountryName.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const countryNames = new Intl.DisplayNames(["en"], { type: "region" });
2+
3+
/**
4+
* Returns the English display name for an alpha-2 (e.g. "GB") country code.
5+
* Falls back to the original input when the code is unrecognised.
6+
*/
7+
export const getCountryName = (countryCode: string): string | undefined => {
8+
return countryNames.of(countryCode.toUpperCase());
9+
};

src/helpers/getFlagEmoji.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { describe, expect, test } from "vitest";
2+
import { getFlagEmoji } from "./getFlagEmoji";
3+
4+
describe("getFlagEmoji", () => {
5+
test("iso2 codes", () => {
6+
expect(getFlagEmoji("GB")).toBe("🇬🇧");
7+
expect(getFlagEmoji("US")).toBe("🇺🇸");
8+
expect(getFlagEmoji("FR")).toBe("🇫🇷");
9+
});
10+
11+
test("case insensitive", () => {
12+
expect(getFlagEmoji("gb")).toBe("🇬🇧");
13+
expect(getFlagEmoji("Gb")).toBe("🇬🇧");
14+
});
15+
16+
test("returns something odd for unknown codes", () => {
17+
expect(getFlagEmoji("XX")).toBe("🇽🇽");
18+
});
19+
});

src/helpers/getFlagEmoji.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Returns the flag emoji for an ISO 3166-1 country code.
3+
* Accepts alpha-2 (e.g. "GB") codes.
4+
* Returns broken emoji when the code is unrecognised.
5+
*/
6+
export const getFlagEmoji = (countryCode: string): string => {
7+
if (countryCode.length !== 2)
8+
throw new Error("Invalid country code, must be 2 characters long");
9+
10+
const codePoints = countryCode
11+
.toUpperCase()
12+
.split("")
13+
.map((char) => 127397 + char.charCodeAt(0));
14+
return String.fromCodePoint(...codePoints);
15+
};

src/helpers/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ export * from "./arrayDiff";
33
export * from "./arrayIntersection";
44
export * from "./capitalize";
55
export * from "./chunkArray";
6-
export * from "./chunkText";
76
export * from "./chunkedAll";
87
export * from "./chunkedAsync";
98
export * from "./chunkedDynamic";
9+
export * from "./chunkText";
1010
export * from "./clamp";
1111
export * from "./cleanSpaces";
1212
export * from "./copyToClipboard";
@@ -20,6 +20,8 @@ export * from "./first";
2020
export * from "./firstKey";
2121
export * from "./firstValue";
2222
export * from "./getCookieByName";
23+
export * from "./getCountryName";
24+
export * from "./getFlagEmoji";
2325
export * from "./getKeys";
2426
export * from "./getUrlSearchParam";
2527
export * from "./getUrlSearchParams";
@@ -37,6 +39,7 @@ export * from "./moveToLast";
3739
export * from "./normalizeNumber";
3840
export * from "./normalizeString";
3941
export * from "./omit";
42+
export * from "./paginationToSkipTake";
4043
export * from "./parseArray";
4144
export * from "./parseDate";
4245
export * from "./parsePrimitive";
@@ -52,6 +55,7 @@ export * from "./setObjectPath";
5255
export * from "./setUrlSearchParams";
5356
export * from "./shuffle";
5457
export * from "./singleton";
58+
export * from "./skipTakeToPagination";
5559
export * from "./sleep";
5660
export * from "./stringify";
5761
export * from "./toggleArrayValue";
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { describe, expect, test } from "vitest";
2+
import { paginationToSkipTake } from "./paginationToSkipTake";
3+
4+
describe("paginationToSkipTake", () => {
5+
test("first page", () => {
6+
expect(paginationToSkipTake({ page: 1, perPage: 10 })).toEqual({
7+
skip: 0,
8+
take: 10,
9+
});
10+
});
11+
12+
test("second page", () => {
13+
expect(paginationToSkipTake({ page: 2, perPage: 10 })).toEqual({
14+
skip: 10,
15+
take: 10,
16+
});
17+
});
18+
19+
test("third page with different perPage", () => {
20+
expect(paginationToSkipTake({ page: 3, perPage: 25 })).toEqual({
21+
skip: 50,
22+
take: 25,
23+
});
24+
});
25+
26+
test("perPage of 1", () => {
27+
expect(paginationToSkipTake({ page: 5, perPage: 1 })).toEqual({
28+
skip: 4,
29+
take: 1,
30+
});
31+
});
32+
});

0 commit comments

Comments
 (0)