-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathfilter.js
More file actions
48 lines (37 loc) · 1.09 KB
/
Copy pathfilter.js
File metadata and controls
48 lines (37 loc) · 1.09 KB
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
import { isLatin, isNonStopWord, isNotNumber } from "text-ai";
function filter (criterion) {
return (values) => new Promise((resolve, reject) => {
setTimeout(() => {
try {
const filtered = values.filter(criterion);
resolve(filtered);
} catch (error) {
reject(error);
}
});
});
}
function exclude (items, filters) {
if (!items || !Array.isArray(items) || items.length === 0) {
return Promise.reject(new Error("Invalid items argument"));
}
if (!filters || !Array.isArray(filters) || filters.length === 0) {
return Promise.reject(new Error("Invalid filters argument"));
}
items = Promise.resolve(items);
const promise = filters.reduce((previous, filter) => {
return previous.then(filter);
}, items);
return promise;
}
const words = ["code", "the", "world", "εντολή", "44"];
const filters = [filter(isLatin), filter(isNonStopWord), filter(isNotNumber)];
exclude(words, filters)
.then((result) => {
console.log(result);
})
.catch((error) => {
console.error(error);
});
// Async output:
// ["code", "world"]