-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
73 lines (68 loc) · 2.34 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import type { UpdateExpressionOptions } from "./types.js";
export const isEmpty = (obj: any) => {
if (obj === null || obj === undefined) return true;
if (Array.isArray(obj) && obj.length === 0) return true;
if (typeof obj === "object" && Object.keys(obj).length === 0) return true;
return false;
};
export function randomId(length = 8) {
let result = "";
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const charactersLength = characters.length;
let counter = 0;
while (counter < length) {
result += characters.charAt(
Math.floor(Math.random() * charactersLength)
);
counter += 1;
}
return result;
}
export function toKeyAndExpName(
val: string,
ExpressionAttributeNames: Record<string, string>,
options?: UpdateExpressionOptions
) {
const id = options?.generateRandomId ? options.generateRandomId() : randomId(5);
const [name, ...rest] = val.split("[");
// const name = val.split('[')[0]
const fixedName = name.split("/")[0].replace(/[-#]/g, '') + id;
ExpressionAttributeNames["#" + fixedName] = name;
return "#" + [fixedName, ...rest].join("["); // val -> name
}
export function handleProjectionExpression(
ExpressionAttributeNames: Record<string, string>,
ProjectionExpression: string,
options?: UpdateExpressionOptions
) {
return ProjectionExpression.split(",")
.map((y) => toKeyAndExpName(y.trim(), ExpressionAttributeNames, options))
.join(",");
}
// I like this function:)
export function handlePath(
path: string,
ExpressionAttributeNames: Record<string, any>,
options?: UpdateExpressionOptions
) {
return path
.split(".")
.map((y) => toKeyAndExpName(y, ExpressionAttributeNames, options))
.join(".");
}
export const attributeNameToPath = (attributeName: string) => {
const arr: (string | number)[] = [];
const components = attributeName.split(".");
for (let c of components) {
const regex = /\[\d+\]/g;
const split = c.split(regex);
const matches = c.match(regex)?.map((x) => parseInt(x.slice(1, -1)));
for (let i = 0; i < split.length; i++) {
arr.push(split[i]);
if (matches && i < matches.length) {
arr.push(matches[i]);
}
}
}
return arr;
};