jsrepo 1.0.0

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,31 @@
1
+ /*
2
+ jsrepo 1.0.0-next.18
3
+ Installed from github/ieedan/std
4
+ 11-18-2024
5
+ */
6
+
7
+ /** Calculates the sum of all elements in the array based on the provided function.
8
+ *
9
+ * @param arr Array of items to be summed.
10
+ * @param fn Summing function
11
+ * @returns
12
+ *
13
+ * ## Examples
14
+ *
15
+ * ```ts
16
+ * const total = arraySum([1, 2, 3, 4, 5], (num) => num);
17
+ *
18
+ * console.log(total); // 15
19
+ * ```
20
+ */
21
+ const arraySum = <T>(arr: T[], fn: (item: T) => number): number => {
22
+ let total = 0;
23
+
24
+ for (const item of arr) {
25
+ total = total + fn(item);
26
+ }
27
+
28
+ return total;
29
+ };
30
+
31
+ export { arraySum };
@@ -0,0 +1,73 @@
1
+ /*
2
+ jsrepo 1.0.0-next.18
3
+ Installed from github/ieedan/std
4
+ 11-18-2024
5
+ */
6
+
7
+ import os from 'node:os';
8
+ import { leftPadMin } from './pad';
9
+
10
+ /** Regex used to split on new lines
11
+ *
12
+ * ```
13
+ * /\n|\r\n/g
14
+ * ```
15
+ */
16
+ export const NEW_LINE_REGEX = /\n|\r\n/g;
17
+
18
+ /** Splits str into an array of lines.
19
+ *
20
+ * @param str
21
+ * @returns
22
+ *
23
+ * ## Usage
24
+ *
25
+ * ```ts
26
+ * lines.split("hello\\nhello\nhello"); // ["hello\\nhello", "hello"]
27
+ * ```
28
+ */
29
+ const get = (str: string): string[] => str.split(NEW_LINE_REGEX);
30
+
31
+ export type Options = {
32
+ lineNumbers: boolean;
33
+ prefix: (line: number, lineCount: number) => string;
34
+ };
35
+
36
+ /** Joins the array of lines back into a string using the platform specific EOL.
37
+ *
38
+ * @param lines
39
+ * @returns
40
+ *
41
+ * ## Usage
42
+ *
43
+ * ```ts
44
+ * lines.join(["1", "2", "3"]); // "1\n2\n3" or on windows "1\r\n2\r\n3"
45
+ *
46
+ * // add line numbers
47
+ * lines.join(["import { } from '.'", "console.log('test')"], { lineNumbers: true });
48
+ * // 1 import { } from '.'
49
+ * // 2 console.log('test')
50
+ *
51
+ * // add a custom prefix
52
+ * lines.join(["import { } from '.'", "console.log('test')"], { prefix: () => " + " });
53
+ * // + import { } from '.'
54
+ * // + console.log('test')
55
+ * ```
56
+ */
57
+ const join = (lines: string[], { lineNumbers = false, prefix }: Partial<Options> = {}): string => {
58
+ let transformed = lines;
59
+
60
+ if (lineNumbers) {
61
+ const length = lines.length.toString().length + 1;
62
+
63
+ transformed = transformed.map((line, i) => `${leftPadMin(`${i + 1}`, length)} ${line}`);
64
+ }
65
+
66
+ if (prefix !== undefined) {
67
+ transformed = transformed.map((line, i) => `${prefix(i, lines.length)}${line}`);
68
+ }
69
+
70
+ return transformed.join(os.EOL);
71
+ };
72
+
73
+ export { get, join };
@@ -0,0 +1,32 @@
1
+ /*
2
+ jsrepo 1.0.0-next.18
3
+ Installed from github/ieedan/std
4
+ 11-18-2024
5
+ */
6
+
7
+ /** Maps the provided map into an array using the provided mapping function.
8
+ *
9
+ * @param map Map to be entered into an array
10
+ * @param fn A mapping function to transform each pair into an item
11
+ * @returns
12
+ *
13
+ * ## Example
14
+ * ```ts
15
+ * console.log(map); // Map(5) { 0 => 5, 1 => 4, 2 => 3, 3 => 2, 4 => 1 }
16
+ *
17
+ * const arr = mapToArray(map, (_, value) => value);
18
+ *
19
+ * console.log(arr); // [5, 4, 3, 2, 1]
20
+ * ```
21
+ */
22
+ const mapToArray = <K, V, T>(map: Map<K, V>, fn: (key: K, value: V) => T): T[] => {
23
+ const items: T[] = [];
24
+
25
+ for (const [key, value] of map) {
26
+ items.push(fn(key, value));
27
+ }
28
+
29
+ return items;
30
+ };
31
+
32
+ export { mapToArray };
@@ -0,0 +1,85 @@
1
+ /*
2
+ jsrepo 1.0.0-next.18
3
+ Installed from github/ieedan/std
4
+ 11-18-2024
5
+ */
6
+
7
+ import { stripAsni } from './strip-ansi';
8
+
9
+ /** Adds the `padWith` (default `' '`) to the string the amount of times specified by the `space` argument
10
+ *
11
+ * @param str String to add padding to
12
+ * @param space Whitespace to add
13
+ * @param padWith Character to use to pad the string
14
+ * @returns
15
+ *
16
+ * ## Usage
17
+ * ```ts
18
+ * const padded = leftPad("Hello", 3, ".");
19
+ *
20
+ * console.log(padded); // '...Hello'
21
+ * ```
22
+ */
23
+ const leftPad = (str: string, space: number, padWith = ' ') => {
24
+ return padWith.repeat(space) + str;
25
+ };
26
+
27
+ /** Adds the `padWith` until the string length matches the `length`
28
+ *
29
+ * @param str
30
+ * @param length
31
+ * @param padWith
32
+ *
33
+ * ## Usage
34
+ * ```ts
35
+ * const padded = leftPadMin("1", 3, ".");
36
+ *
37
+ * console.log(padded); // '..1'
38
+ * ```
39
+ */
40
+ const leftPadMin = (str: string, length: number, padWith = ' ') => {
41
+ if (stripAsni(str).length > length)
42
+ throw new Error('String length is greater than the length provided.');
43
+
44
+ return padWith.repeat(length - stripAsni(str).length) + str;
45
+ };
46
+
47
+ /** Adds the `padWith` (default `' '`) to the string the amount of times specified by the `space` argument
48
+ *
49
+ * @param str String to add padding to
50
+ * @param space Whitespace to add
51
+ * @param padWith Character to use to pad the string
52
+ * @returns
53
+ *
54
+ * ## Usage
55
+ * ```ts
56
+ * const padded = rightPad("Hello", 3, ".");
57
+ *
58
+ * console.log(padded); // 'Hello...'
59
+ * ```
60
+ */
61
+ const rightPad = (str: string, space: number, padWith = ' ') => {
62
+ return str + padWith.repeat(space);
63
+ };
64
+
65
+ /** Adds the `padWith` until the string length matches the `length`
66
+ *
67
+ * @param str
68
+ * @param length
69
+ * @param padWith
70
+ *
71
+ * ## Usage
72
+ * ```ts
73
+ * const padded = rightPadMin("1", 3, ".");
74
+ *
75
+ * console.log(padded); // '1..'
76
+ * ```
77
+ */
78
+ const rightPadMin = (str: string, length: number, padWith = ' ') => {
79
+ if (stripAsni(str).length > length)
80
+ throw new Error('String length is greater than the length provided.');
81
+
82
+ return str + padWith.repeat(length - stripAsni(str).length);
83
+ };
84
+
85
+ export { leftPad, leftPadMin, rightPad, rightPadMin };
@@ -0,0 +1,25 @@
1
+ /*
2
+ jsrepo 1.0.0-next.18
3
+ Installed from github/ieedan/std
4
+ 11-18-2024
5
+ */
6
+
7
+ import ansiRegex from 'ansi-regex';
8
+
9
+ /** Strips a string with ansi escape codes back to it's original form. Useful for when you need to get the actual length of a string.
10
+ *
11
+ * @param str
12
+ * @returns
13
+ *
14
+ * ## Usage
15
+ * ```ts
16
+ * import color from "chalk";
17
+ *
18
+ * const redString = color.red(redString);
19
+ *
20
+ * stripAnsi(redString);
21
+ * ```
22
+ */
23
+ const stripAsni = (str: string) => str.replace(ansiRegex(), '');
24
+
25
+ export { stripAsni };