Skip to content

Commit 7ad3517

Browse files
committed
first complete version
1 parent 2418f73 commit 7ad3517

File tree

218 files changed

+5013
-644
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

218 files changed

+5013
-644
lines changed

docs/CSVToArray.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ Use `String.prototype.split('\n')` to create a string for each row, then `String
1414
Omit the second argument, `delimiter`, to use a default delimiter of `,`.
1515
Omit the third argument, `omitFirstRow`, to include the first row (title row) of the CSV string.
1616

17-
```ts
17+
```ts title="typescript"
1818
const CSVToArray = (data: string, delimiter = ",", omitFirstRow = false) =>
1919
data
2020
.slice(omitFirstRow ? data.indexOf("\n") + 1 : 0)
2121
.split("\n")
2222
.map((v) => v.split(delimiter));
2323
```
2424

25-
```ts
25+
```ts title="typescript"
2626
CSVToArray("a,b\nc,d"); // [['a','b'],['c','d']];
2727
CSVToArray("a;b\nc;d", ";"); // [['a','b'],['c','d']];
2828
CSVToArray("col1,col2\na,b\nc,d", ",", true); // [['a','b'],['c','d']];

docs/CSVToJSON.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Use `String.prototype.split('\n')` to create a string for each row, then `Array.
1515
Use `Array.prototype.reduce()` to create an object for each row's values, with the keys parsed from the title row.
1616
Omit the second argument, `delimiter`, to use a default delimiter of `,`.
1717

18-
```ts
18+
```ts title="typescript"
1919
type StringMap<T = string> = { [key: string]: T };
2020

2121
const CSVToJSON = (data: string, delimiter = ",") => {
@@ -33,7 +33,7 @@ const CSVToJSON = (data: string, delimiter = ",") => {
3333
};
3434
```
3535

36-
```ts
36+
```ts title="typescript"
3737
CSVToJSON("col1,col2\na,b\nc,d"); // [{'col1': 'a', 'col2': 'b'}, {'col1': 'c', 'col2': 'd'}];
3838
CSVToJSON("col1;col2\na;b\nc;d", ";"); // [{'col1': 'a', 'col2': 'b'}, {'col1': 'c', 'col2': 'd'}];
3939
```

docs/JSONtoCSV.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Use `Array.prototype.map()` and `Array.prototype.reduce()` to create a row for e
1414
Use `Array.prototype.join('\n')` to combine all rows into a string.
1515
Omit the third argument, `delimiter`, to use a default delimiter of `,`.
1616

17-
```ts
17+
```ts title="typescript"
1818
const JSONtoCSV = (arr: any[], columns: string[], delimiter = ",") =>
1919
[
2020
columns.join(delimiter),
@@ -28,7 +28,7 @@ const JSONtoCSV = (arr: any[], columns: string[], delimiter = ",") =>
2828
].join("\n");
2929
```
3030

31-
```ts
31+
```ts title="typescript"
3232
JSONtoCSV(
3333
[{ a: 1, b: 2 }, { a: 3, b: 4, c: 5 }, { a: 6 }, { b: 7 }],
3434
["a", "b"]

docs/RGBToHex.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ Converts the values of RGB components to a color code.
1111

1212
Convert given RGB parameters to hexadecimal string using bitwise left-shift operator (`<<`) and `toString(16)`, then `String.padStart(6,'0')` to get a 6-digit hexadecimal value.
1313

14-
```ts
14+
```ts title="typescript"
1515
const RGBToHex = (r: number, g: number, b: number, hash: "#" | "" = "") =>
1616
hash + ((r << 16) + (g << 8) + b).toString(16).padStart(6, "0");
1717
```
1818

19-
```ts
19+
```ts title="typescript"
2020
RGBToHex(255, 165, 1); // 'ffa501'
2121
RGBToHex(255, 165, 1, "#"); // '#ffa501'
2222
```

docs/URLJoin.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ title: URLJoin
33
tags: string,regexp,advanced
44
---
55

6+
![TS](https://img.shields.io/badge/supports-typescript-blue.svg?style=flat-square)
67
![JS](https://img.shields.io/badge/supports-javascript-yellow.svg?style=flat-square)
7-
![TODO](https://img.shields.io/badge///TODO-blue.svg?style=flat-square)
8+
![Deno](https://img.shields.io/badge/supports-deno-green.svg?style=flat-square)
89

910
Joins all given URL segments together, then normalizes the resulting URL.
1011

1112
Use `String.prototype.join('/')` to combine URL segments, then a series of `String.prototype.replace()` calls with various regexps to normalize the resulting URL (remove double slashes, add proper slashes for protocol, remove slashes before parameters, combine parameters with `'&'` and normalize first parameter delimiter).
1213

13-
```js
14-
const URLJoin = (...args) =>
14+
```ts title="typescript"
15+
const URLJoin = (...args: string[]) =>
1516
args
1617
.join("/")
1718
.replace(/[\/]+/g, "/")
@@ -20,8 +21,17 @@ const URLJoin = (...args) =>
2021
.replace(/\/(\?|&|#[^!])/g, "$1")
2122
.replace(/\?/g, "&")
2223
.replace("&", "?");
24+
25+
const URLJoinWithParams = (url: string, params: AnyObject<string | number>) => {
26+
return URLJoin(url, objectToQueryString(params));
27+
};
2328
```
2429

25-
```js
30+
```ts title="typescript"
2631
URLJoin("http://www.google.com", "a", "/b/cd", "?foo=123", "?bar=foo"); // 'http://www.google.com/a/b/cd?foo=123&bar=foo'
32+
33+
URLJoinWithParams(URLJoin("http://www.google.com", "a", "/b/cd"), {
34+
foo: 123,
35+
bar: "foo",
36+
}); // "http://www.google.com/a/b/cd?foo=123&bar=foo";
2737
```

docs/UUIDGeneratorBrowser.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,24 @@ title: UUIDGeneratorBrowser
33
tags: browser,random,intermediate
44
---
55

6+
![TS](https://img.shields.io/badge/supports-typescript-blue.svg?style=flat-square)
67
![JS](https://img.shields.io/badge/supports-javascript-yellow.svg?style=flat-square)
7-
![TODO](https://img.shields.io/badge///TODO-blue.svg?style=flat-square)
8+
![Deno](https://img.shields.io/badge/supports-deno-green.svg?style=flat-square)
89

910
Generates a UUID in a browser.
1011

1112
Use `crypto` API to generate a UUID, compliant with [RFC4122](https://www.ietf.org/rfc/rfc4122.txt) version 4.
1213

13-
```js
14+
```ts title="typescript"
1415
const UUIDGeneratorBrowser = () =>
15-
([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c) =>
16+
(String(1e7) + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c: string) =>
1617
(
17-
c ^
18-
(crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))
18+
Number(c) ^
19+
(crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (Number(c) / 4)))
1920
).toString(16)
2021
);
2122
```
2223

23-
```js
24+
```ts title="typescript"
2425
UUIDGeneratorBrowser(); // '7982fcfe-5721-4632-bede-6000885be57d'
2526
```

docs/UUIDGeneratorNode.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ Generates a UUID in Node.JS.
1010

1111
Use `crypto` API to generate a UUID, compliant with [RFC4122](https://www.ietf.org/rfc/rfc4122.txt) version 4.
1212

13-
```js
13+
```ts title="typescript"
1414
const crypto = require("crypto");
15-
const UUIDGeneratorNode = () =>
16-
([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c) =>
17-
(c ^ (crypto.randomBytes(1)[0] & (15 >> (c / 4)))).toString(16)
15+
const UUIDGeneratorBrowser = () =>
16+
(String(1e7) + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c: string) =>
17+
(Number(c) ^ (crypto.randomBytes(1)[0] & (15 >> (Number(c) / 4)))).toString(
18+
16
19+
)
1820
);
1921
```
2022

21-
```js
23+
```ts title="typescript"
2224
UUIDGeneratorNode(); // '79c7c136-60ee-40a2-beb2-856f1feabefc'
2325
```

docs/accumulate.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ Returns an array of partial sums.
1111

1212
Use `Array.prototype.reduce()`, `Array.prototype.slice(-1)` and the unary `+` operator to add each value to the unary array containing the previous sum.
1313

14-
```ts
14+
```ts title="typescript"
1515
const accumulate = (...nums: number[]): number[] =>
1616
nums.reduce((acc: number[], n) => [...acc, n + +acc.slice(-1)], []);
1717
```
1818

19-
```ts
19+
```ts title="typescript"
2020
accumulate(1, 2, 3, 4); // [1, 3, 6, 10]
2121
accumulate(...[1, 2, 3, 4]); // [1, 3, 6, 10]
2222
```

docs/all.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ Returns `true` if the provided predicate function returns `true` for all element
1212
Use `Array.prototype.every()` to test if all elements in the collection return `true` based on `fn`.
1313
Omit the second argument, `fn`, to use `Boolean` as a default.
1414

15-
```ts
15+
```ts title="typescript"
1616
const all = <T = any>(arr: T[], fn: (t: T) => boolean = Boolean) =>
1717
arr.every(fn);
1818
```
1919

20-
```ts
20+
```ts title="typescript"
2121
assertEquals(all([1, 2, 3, 4]), true);
2222
assertEquals(all([2, null, 1]), false);
2323

docs/allEqual.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ Check if all elements in an array are equal.
1212
Use `Array.prototype.every()` to check if all the elements of the array are the same as the first one.
1313
Elements in the array are compared using the strict comparison operator, which does not account for `NaN` self-inequality.
1414

15-
```ts
15+
```ts title="typescript"
1616
const allEqual = <T = any>(arr: T[]) => arr.every((val) => val === arr[0]);
1717
```
1818

19-
```ts
19+
```ts title="typescript"
2020
allEqual([1, 2, 3, 4, 5, 6]); // false
2121
allEqual([1, 1, 1, 1]); // true
2222
```

0 commit comments

Comments
 (0)