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
import { fillArray } from "https://esm.town/v/kajgod/fillArray";
interface IDetails {
title: string;
price: number;
url: string;
}
interface INjuskaloData {
keywords: string[];
registeredTitles: string[];
sentData: IDetails[];
}
export const njuskaloCreateDataWithInterfaces = (njuskaloData: INjuskaloData) => {
let data: INjuskaloData;
const emptyDetails: IDetails = {
title: "",
price: 0,
url: "",
};
// =========================================================================
// fill data with emty values
// =========================================================================
const fillEmpty = (len: number) => ({
keywords: fillArray("", len),
registeredTitles: fillArray("", len),
sentData: fillArray(emptyDetails, len),
});
// =========================================================================
// update permanent data with new keywords
// preserve old data if keywords are repeated
// =========================================================================
const newKeywords = (keywords: string[]) => {
const data = njuskaloData as INjuskaloData;
const len = keywords?.length || 0;
const newKeywords: string[] = keywords;
const newRegisteredTitles: string[] = fillArray("", len);
const newSentData: IDetails[] = fillArray(
emptyDetails,
len,
);
keywords?.forEach((w) => {
const index = data?.keywords?.indexOf(w) || -1;
if (index >= 0) {
newRegisteredTitles[index] = data.registeredTitles[index];
newSentData[index] = data.sentData[index];
}
});
data.keywords = newKeywords;
data.registeredTitles = newRegisteredTitles;
data.sentData = newSentData;
return {
data,
len,
};
};
return {
data,
emptyDetails,
fillEmpty,
newKeywords,
defaultData: njuskaloData as INjuskaloData,
};
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023