Fixed 267U.pre2
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
/**
|
||||
* Count the number of elements that satisfy the predicate
|
||||
*/ export function countIf(f, xs) {
|
||||
return xs.filter(f).length;
|
||||
}
|
||||
/**
|
||||
* Count the number of elements that is equal to the element
|
||||
*/ export function count(a, xs) {
|
||||
return countIf((x)=>x === a, xs);
|
||||
}
|
||||
/**
|
||||
* Concatenate an array of arrays
|
||||
*/ export function concat(xss) {
|
||||
return [].concat(...xss);
|
||||
}
|
||||
/**
|
||||
* Intersperse the element between the elements of the array
|
||||
* @param sep The element to be interspersed
|
||||
*/ export function intersperse(sep, xs) {
|
||||
return concat(xs.map((x)=>[
|
||||
sep,
|
||||
x
|
||||
])).slice(1);
|
||||
}
|
||||
/**
|
||||
* Returns the array of elements that is not equal to the element
|
||||
*/ export function erase(a, xs) {
|
||||
return xs.filter((x)=>x !== a);
|
||||
}
|
||||
/**
|
||||
* Finds the array of all elements in the first array not contained in the second array.
|
||||
* The order of result values are determined by the first array.
|
||||
*/ export function difference(xs, ys) {
|
||||
return xs.filter((x)=>!ys.includes(x));
|
||||
}
|
||||
/**
|
||||
* Remove all but the first element from every group of equivalent elements
|
||||
*/ export function unique(xs) {
|
||||
return [
|
||||
...new Set(xs)
|
||||
];
|
||||
}
|
||||
export function uniqBy(a, key) {
|
||||
const seen = new Set();
|
||||
return a.filter(function(item) {
|
||||
const k = key(item);
|
||||
return seen.has(k) ? false : seen.add(k);
|
||||
});
|
||||
}
|
||||
export function sum(xs) {
|
||||
return xs.reduce((a, b)=>a + b, 0);
|
||||
}
|
||||
export function maximum(xs) {
|
||||
return Math.max(...xs);
|
||||
}
|
||||
/**
|
||||
* Splits an array based on the equivalence relation.
|
||||
* The concatenation of the result is equal to the argument.
|
||||
*/ export function groupBy(f, xs) {
|
||||
const groups = [];
|
||||
for (const x of xs){
|
||||
if (groups.length !== 0 && f(groups[groups.length - 1][0], x)) {
|
||||
groups[groups.length - 1].push(x);
|
||||
} else {
|
||||
groups.push([
|
||||
x
|
||||
]);
|
||||
}
|
||||
}
|
||||
return groups;
|
||||
}
|
||||
/**
|
||||
* Splits an array based on the equivalence relation induced by the function.
|
||||
* The concatenation of the result is equal to the argument.
|
||||
*/ export function groupOn(f, xs) {
|
||||
return groupBy((a, b)=>f(a) === f(b), xs);
|
||||
}
|
||||
export function groupByX(collections, keySelector) {
|
||||
return collections.reduce((obj, item)=>{
|
||||
const key = keySelector(item);
|
||||
if (!Object.prototype.hasOwnProperty.call(obj, key)) {
|
||||
obj[key] = [];
|
||||
}
|
||||
obj[key].push(item);
|
||||
return obj;
|
||||
}, {});
|
||||
}
|
||||
/**
|
||||
* Compare two arrays by lexicographical order
|
||||
*/ export function lessThan(xs, ys) {
|
||||
for(let i = 0; i < Math.min(xs.length, ys.length); i++){
|
||||
if (xs[i] < ys[i]) return true;
|
||||
if (xs[i] > ys[i]) return false;
|
||||
}
|
||||
return xs.length < ys.length;
|
||||
}
|
||||
/**
|
||||
* Returns the longest prefix of elements that satisfy the predicate
|
||||
*/ export function takeWhile(f, xs) {
|
||||
const ys = [];
|
||||
for (const x of xs){
|
||||
if (f(x)) {
|
||||
ys.push(x);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ys;
|
||||
}
|
||||
export function cumulativeSum(xs) {
|
||||
const ys = Array.from(xs); // deep copy
|
||||
for(let i = 1; i < ys.length; i++)ys[i] += ys[i - 1];
|
||||
return ys;
|
||||
}
|
||||
export function toArray(x) {
|
||||
return Array.isArray(x) ? x : x != null ? [
|
||||
x
|
||||
] : [];
|
||||
}
|
||||
export function toSingle(x) {
|
||||
return Array.isArray(x) ? x[0] : x;
|
||||
}
|
||||
export function toSingleLast(x) {
|
||||
return Array.isArray(x) ? x.at(-1) : x;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
export async function awaitAll(obj) {
|
||||
const target = {};
|
||||
const keys = Object.keys(obj);
|
||||
const values = Object.values(obj);
|
||||
const resolvedValues = await Promise.all(values.map((value)=>!value?.constructor || value.constructor.name !== "Object" ? value : awaitAll(value)));
|
||||
for(let i = 0; i < keys.length; i++){
|
||||
target[keys[i]] = resolvedValues[i];
|
||||
}
|
||||
return target;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export function gcd(a, b) {
|
||||
return b === 0 ? a : gcd(b, a % b);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
export function just(value) {
|
||||
return {
|
||||
isJust: ()=>true,
|
||||
get: ()=>value
|
||||
};
|
||||
}
|
||||
export function nothing() {
|
||||
return {
|
||||
isJust: ()=>false
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// Returns T if promise settles before timeout, otherwise returns void, finishing execution in the background.
|
||||
export async function promiseEarlyReturn(promise, after) {
|
||||
const timer = new Promise((res)=>setTimeout(()=>res(undefined), after));
|
||||
return Promise.race([
|
||||
promise,
|
||||
timer
|
||||
]);
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export { };
|
||||
@@ -0,0 +1,12 @@
|
||||
export function concat(xs) {
|
||||
return xs.join("");
|
||||
}
|
||||
export function capitalize(s) {
|
||||
return toUpperCase(s.charAt(0)) + toLowerCase(s.slice(1));
|
||||
}
|
||||
export function toUpperCase(s) {
|
||||
return s.toUpperCase();
|
||||
}
|
||||
export function toLowerCase(s) {
|
||||
return s.toLowerCase();
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export const fallback = Symbol("fallback");
|
||||
@@ -0,0 +1,25 @@
|
||||
const dateTimeIntervals = {
|
||||
day: 86400000,
|
||||
hour: 3600000,
|
||||
ms: 1
|
||||
};
|
||||
export function dateUTC(time) {
|
||||
const d = time.length === 2 ? Date.UTC(time[0], time[1]) : time.length === 3 ? Date.UTC(time[0], time[1], time[2]) : time.length === 4 ? Date.UTC(time[0], time[1], time[2], time[3]) : time.length === 5 ? Date.UTC(time[0], time[1], time[2], time[3], time[4]) : time.length === 6 ? Date.UTC(time[0], time[1], time[2], time[3], time[4], time[5]) : time.length === 7 ? Date.UTC(time[0], time[1], time[2], time[3], time[4], time[5], time[6]) : null;
|
||||
if (!d) throw new Error("wrong number of arguments");
|
||||
return new Date(d);
|
||||
}
|
||||
export function isTimeSame(a, b) {
|
||||
return a.getTime() === b.getTime();
|
||||
}
|
||||
export function isTimeBefore(a, b) {
|
||||
return a.getTime() - b.getTime() < 0;
|
||||
}
|
||||
export function isTimeAfter(a, b) {
|
||||
return a.getTime() - b.getTime() > 0;
|
||||
}
|
||||
export function addTime(x, value, span = "ms") {
|
||||
return new Date(x.getTime() + value * dateTimeIntervals[span]);
|
||||
}
|
||||
export function subtractTime(x, value, span = "ms") {
|
||||
return new Date(x.getTime() - value * dateTimeIntervals[span]);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
export function query(obj) {
|
||||
const params = Object.entries(obj).filter(([, v])=>Array.isArray(v) ? v.length : v !== undefined).reduce((a, [k, v])=>(a[k] = v, a), {});
|
||||
return Object.entries(params).map((e)=>`${e[0]}=${encodeURIComponent(e[1])}`).join("&");
|
||||
}
|
||||
export function appendQuery(url, query) {
|
||||
return `${url}${/\?/.test(url) ? url.endsWith("?") ? "" : "&" : "?"}${query}`;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
const map = {
|
||||
"&": "&",
|
||||
"<": "<",
|
||||
">": ">",
|
||||
'"': """,
|
||||
"'": "'"
|
||||
};
|
||||
const beginingOfCDATA = "<![CDATA[";
|
||||
const endOfCDATA = "]]>";
|
||||
export function escapeValue(x) {
|
||||
let insideOfCDATA = false;
|
||||
let builder = "";
|
||||
for(let i = 0; i < x.length;){
|
||||
if (insideOfCDATA) {
|
||||
if (x.slice(i, i + beginingOfCDATA.length) === beginingOfCDATA) {
|
||||
insideOfCDATA = true;
|
||||
i += beginingOfCDATA.length;
|
||||
} else {
|
||||
builder += x[i++];
|
||||
}
|
||||
} else {
|
||||
if (x.slice(i, i + endOfCDATA.length) === endOfCDATA) {
|
||||
insideOfCDATA = false;
|
||||
i += endOfCDATA.length;
|
||||
} else {
|
||||
const b = x[i++];
|
||||
builder += map[b] || b;
|
||||
}
|
||||
}
|
||||
}
|
||||
return builder;
|
||||
}
|
||||
export function escapeAttribute(x) {
|
||||
return Object.entries(map).reduce((a, [k, v])=>a.replace(k, v), x);
|
||||
}
|
||||
Reference in New Issue
Block a user