@pogodisco/ts-kit 0.0.1 → 0.2.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.
@@ -4,3 +4,5 @@ export * from "./push-unique-to-array.js";
4
4
  export * from "./get-at-path.js";
5
5
  export * from "./set-at-path.js";
6
6
  export * from "./select-map.js";
7
+ export * from "./set-if-present.js";
8
+ export * from "./set-when.js";
@@ -4,3 +4,5 @@ export * from "./push-unique-to-array.js";
4
4
  export * from "./get-at-path.js";
5
5
  export * from "./set-at-path.js";
6
6
  export * from "./select-map.js";
7
+ export * from "./set-if-present.js";
8
+ export * from "./set-when.js";
@@ -0,0 +1,3 @@
1
+ export declare function setIfPresent<T>(key: string, value: T | null | undefined): {
2
+ [key]: NonNullable<T>;
3
+ };
@@ -0,0 +1,3 @@
1
+ export function setIfPresent(key, value) {
2
+ return value == null ? {} : { [key]: value };
3
+ }
@@ -0,0 +1,2 @@
1
+ export declare function setWhen<T extends object>(condition: boolean, value: T | undefined): T | undefined;
2
+ export declare function whenDo<T extends object>(condition: boolean, factory: () => T): T | undefined;
@@ -0,0 +1,6 @@
1
+ export function setWhen(condition, value) {
2
+ return condition ? value : undefined;
3
+ }
4
+ export function whenDo(condition, factory) {
5
+ return condition ? factory() : undefined;
6
+ }
@@ -1,2 +1,3 @@
1
1
  export * from "./random-from-array.js";
2
2
  export * from "./random-subset-from-array.js";
3
+ export * from "./pipe-array.js";
@@ -1,2 +1,3 @@
1
1
  export * from "./random-from-array.js";
2
2
  export * from "./random-subset-from-array.js";
3
+ export * from "./pipe-array.js";
@@ -0,0 +1,4 @@
1
+ export declare const pipeArray: <I, O>(input: {
2
+ items: I[];
3
+ fns: Array<(item: any, index: number) => any>;
4
+ }) => Promise<O[]>;
@@ -0,0 +1,11 @@
1
+ export const pipeArray = async (input) => {
2
+ const out = [];
3
+ for (let i = 0; i < input.items.length; i++) {
4
+ let acc = input.items[i];
5
+ for (const fn of input.fns) {
6
+ acc = await fn(acc, i);
7
+ }
8
+ out.push(acc);
9
+ }
10
+ return out;
11
+ };
@@ -0,0 +1,23 @@
1
+ import { SanitizerEntry } from "./sanitize.js";
2
+ export declare function oid(value: string | string[], fallback?: any): any;
3
+ export declare function numberDecimal(value: string | number, fallback?: string | number): {
4
+ $numberDecimal: string | number;
5
+ };
6
+ export declare function date(value: string | Date): {
7
+ $date: string | Date;
8
+ };
9
+ export declare function numberInt(value: string | number, fallback?: string | number): {
10
+ $numberInt: string | number;
11
+ };
12
+ export declare function numberLong(value: string | number, fallback?: string | number): {
13
+ $numberLong: string | number;
14
+ };
15
+ export declare function uppercase(value: string | string[], fallback?: any): any;
16
+ export declare function lowercase(value: string | string[], fallback?: any): any;
17
+ export declare const toDecimal: (key: string) => SanitizerEntry;
18
+ export declare const toInt: (key: string) => SanitizerEntry;
19
+ export declare const toOid: (key: string) => SanitizerEntry;
20
+ export declare const toDate: (key: string) => SanitizerEntry;
21
+ export declare const toLong: (key: string) => SanitizerEntry;
22
+ export declare const toLc: (key: string) => SanitizerEntry;
23
+ export declare const toUc: (key: string) => SanitizerEntry;
@@ -0,0 +1,72 @@
1
+ export function oid(value, fallback = null) {
2
+ if (!value || (typeof value === "string" && value.trim() === ""))
3
+ return fallback;
4
+ if (Array.isArray(value)) {
5
+ if (!value.length)
6
+ return [];
7
+ return value.map((v) => ({
8
+ $oid: v,
9
+ }));
10
+ }
11
+ return { $oid: value };
12
+ }
13
+ export function numberDecimal(value, fallback = 0) {
14
+ return { $numberDecimal: value || fallback };
15
+ }
16
+ export function date(value) {
17
+ return { $date: value };
18
+ }
19
+ export function numberInt(value, fallback = 0) {
20
+ return { $numberInt: value || fallback };
21
+ }
22
+ export function numberLong(value, fallback = 0) {
23
+ return { $numberLong: value || fallback };
24
+ }
25
+ export function uppercase(value, fallback = null) {
26
+ if (!value || (typeof value === "string" && value.trim() === ""))
27
+ return fallback;
28
+ if (Array.isArray(value)) {
29
+ if (!value.length)
30
+ return [];
31
+ return value.map((v) => v.toUpperCase());
32
+ }
33
+ return value.toUpperCase();
34
+ }
35
+ export function lowercase(value, fallback = null) {
36
+ if (!value || (typeof value === "string" && value.trim() === ""))
37
+ return fallback;
38
+ if (Array.isArray(value)) {
39
+ if (!value.length)
40
+ return [];
41
+ return value.map((v) => v.toLowerCase());
42
+ }
43
+ return value.toLowerCase();
44
+ }
45
+ export const toDecimal = (key) => [
46
+ key,
47
+ ({ value }) => numberDecimal(value),
48
+ ];
49
+ export const toInt = (key) => [
50
+ key,
51
+ ({ value }) => numberInt(value),
52
+ ];
53
+ export const toOid = (key) => [
54
+ key,
55
+ ({ value }) => oid(value),
56
+ ];
57
+ export const toDate = (key) => [
58
+ key,
59
+ ({ value }) => date(value),
60
+ ];
61
+ export const toLong = (key) => [
62
+ key,
63
+ ({ value }) => numberLong(value),
64
+ ];
65
+ export const toLc = (key) => [
66
+ key,
67
+ ({ value }) => lowercase(value),
68
+ ];
69
+ export const toUc = (key) => [
70
+ key,
71
+ ({ value }) => uppercase(value),
72
+ ];
@@ -10,3 +10,4 @@ export * from "./format-unit-suffix.js";
10
10
  export * from "./sanitize.js";
11
11
  export * from "./truncate-text.js";
12
12
  export * from "./to-kebab-case.js";
13
+ export * from "./extended-json.js";
@@ -10,3 +10,4 @@ export * from "./format-unit-suffix.js";
10
10
  export * from "./sanitize.js";
11
11
  export * from "./truncate-text.js";
12
12
  export * from "./to-kebab-case.js";
13
+ export * from "./extended-json.js";
package/dist/index.d.ts CHANGED
@@ -6,3 +6,4 @@ export * from "./processors/index.js";
6
6
  export * from "./array/index.js";
7
7
  export * from "./date/index.js";
8
8
  export * from "./finance/index.js";
9
+ export * from "./lang/index.js";
package/dist/index.js CHANGED
@@ -6,3 +6,4 @@ export * from "./processors/index.js";
6
6
  export * from "./array/index.js";
7
7
  export * from "./date/index.js";
8
8
  export * from "./finance/index.js";
9
+ export * from "./lang/index.js";
@@ -0,0 +1 @@
1
+ export * from "./pl/index.js";
@@ -0,0 +1 @@
1
+ export * from "./pl/index.js";
@@ -0,0 +1 @@
1
+ export * from "./noun-form.js";
@@ -0,0 +1 @@
1
+ export * from "./noun-form.js";
@@ -0,0 +1 @@
1
+ export declare function nounForm(n: number, forms: [string, string, string]): string;
@@ -0,0 +1,10 @@
1
+ export function nounForm(n, forms) {
2
+ const absN = Math.abs(n);
3
+ const lastTwo = absN % 100;
4
+ const lastDigit = absN % 10;
5
+ if (lastDigit === 1 && !(lastTwo >= 11 && lastTwo <= 14))
6
+ return forms[0]; // singular
7
+ if (lastDigit >= 2 && lastDigit <= 4 && !(lastTwo >= 12 && lastTwo <= 14))
8
+ return forms[1]; // few
9
+ return forms[2]; // many
10
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pogodisco/ts-kit",
3
- "version": "0.0.1",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -13,7 +13,6 @@
13
13
  "utilities",
14
14
  "typescript"
15
15
  ],
16
- "license": "MIT",
17
16
  "main": "./dist/index.js",
18
17
  "types": "./dist/index.d.ts",
19
18
  "exports": {