🔥 Remove extensions

This commit is contained in:
Iván Ovejero 2023-01-19 11:41:01 +01:00
parent 4ebf40c7a2
commit d9075d89ef
8 changed files with 2 additions and 207 deletions

View File

@ -121,7 +121,7 @@ function pluck(value: unknown[], extraArgs: unknown[]): unknown[] {
}) as unknown[];
}
function random(value: unknown[]): unknown {
function randomItem(value: unknown[]): unknown {
const len = value === undefined ? 0 : value.length;
return len ? value[Math.floor(Math.random() * len)] : undefined;
}
@ -241,26 +241,6 @@ function chunk(value: unknown[], extraArgs: number[]) {
return chunks;
}
function filter(value: unknown[], extraArgs: unknown[]): unknown[] {
const [field, term] = extraArgs as [string | (() => void), unknown | string];
if (typeof field !== 'string' && typeof field !== 'function') {
throw new ExpressionExtensionError(
'filter requires 1 or 2 arguments: (field and term), (term and [optional keepOrRemove "keep" or "remove" default "keep"] (for string arrays)), or function. e.g. .filter("type", "home") or .filter((i) => i.type === "home") or .filter("home", [optional keepOrRemove]) (for string arrays)',
);
}
if (value.every((i) => typeof i === 'string') && typeof field === 'string') {
return (value as string[]).filter((i) =>
term === 'remove' ? !i.includes(field) : i.includes(field),
);
} else if (typeof field === 'string') {
return value.filter(
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
(v) => typeof v === 'object' && v !== null && field in v && (v as any)[field] === term,
);
}
return value.filter(field);
}
function renameKeys(value: unknown[], extraArgs: string[]): unknown[] {
if (extraArgs.length === 0 || extraArgs.length % 2 !== 0) {
throw new ExpressionExtensionError(
@ -369,15 +349,12 @@ export const arrayExtensions: ExtensionMap = {
functions: {
count: length,
duplicates: unique,
filter,
first,
last,
length,
pluck,
unique,
random,
randomItem: random,
remove: unique,
randomItem,
size: length,
sum,
min,

View File

@ -4,7 +4,6 @@ import {
DateTime,
DateTimeFormatOptions,
DateTimeUnit,
Duration,
DurationLike,
DurationObjectUnits,
LocaleOptions,
@ -200,62 +199,6 @@ function toLocaleString(date: Date | DateTime, extraArgs: unknown[]): string {
return DateTime.fromJSDate(date).toLocaleString(dateFormat, { locale });
}
function toTimeFromNow(date: Date): string {
let diffObj: Duration;
if (isDateTime(date)) {
diffObj = date.diffNow();
} else {
diffObj = DateTime.fromJSDate(date).diffNow();
}
const as = (unit: DurationUnit) => {
return Math.round(Math.abs(diffObj.as(unit)));
};
if (as('years')) {
return `${as('years')} years ago`;
}
if (as('months')) {
return `${as('months')} months ago`;
}
if (as('weeks')) {
return `${as('weeks')} weeks ago`;
}
if (as('days')) {
return `${as('days')} days ago`;
}
if (as('hours')) {
return `${as('hours')} hours ago`;
}
if (as('minutes')) {
return `${as('minutes')} minutes ago`;
}
if (as('seconds') && as('seconds') > 10) {
return `${as('seconds')} seconds ago`;
}
return 'just now';
}
function timeTo(date: Date | DateTime, extraArgs: unknown[]): Duration {
const [diff = new Date().toISOString(), unit = 'seconds'] = extraArgs as [string, DurationUnit];
const diffDate = new Date(diff);
if (isDateTime(date)) {
return date.diff(DateTime.fromJSDate(diffDate), DURATION_MAP[unit] || unit);
}
return DateTime.fromJSDate(date).diff(DateTime.fromJSDate(diffDate), DURATION_MAP[unit] || unit);
}
function toDate(date: Date | DateTime) {
if (isDateTime(date)) {
return date.set({ hour: 0, minute: 0, second: 0, millisecond: 0 }).toJSDate();
}
let datetime = DateTime.fromJSDate(date);
if (date.getTimezoneOffset() === 0) {
datetime = datetime.setZone('UTC');
}
return datetime.set({ hour: 0, minute: 0, second: 0, millisecond: 0 }).toJSDate();
}
export const dateExtensions: ExtensionMap = {
typeName: 'Date',
functions: {
@ -268,10 +211,7 @@ export const dateExtensions: ExtensionMap = {
isWeekend,
minus,
plus,
toTimeFromNow,
timeTo,
format,
toLocaleString,
toDate,
},
};

View File

@ -21,18 +21,6 @@ function isPresent(value: number): boolean {
return !isBlank(value);
}
function random(value: number): number {
return Math.floor(Math.random() * value);
}
function isTrue(value: number) {
return value === 1;
}
function isFalse(value: number) {
return value === 0;
}
function isEven(value: number) {
return value % 2 === 0;
}
@ -60,13 +48,8 @@ export const numberExtensions: ExtensionMap = {
ceil,
floor,
format,
random,
round,
isBlank,
isPresent,
isTrue,
isNotTrue: isFalse,
isFalse,
isEven,
isOdd,
},

View File

@ -34,9 +34,6 @@ const URL_REGEXP =
const CHAR_TEST_REGEXP = /\p{L}/u;
const PUNC_TEST_REGEXP = /[!?.]/;
const TRUE_VALUES = ['true', '1', 't', 'yes', 'y'];
const FALSE_VALUES = ['false', '0', 'f', 'no', 'n'];
function encrypt(value: string, extraArgs?: unknown): string {
const [format = 'MD5'] = extraArgs as string[];
if (format.toLowerCase() === 'base64') {
@ -166,14 +163,6 @@ function quote(value: string, extraArgs: string[]) {
.replace(new RegExp(`\\${quoteChar}`, 'g'), `\\${quoteChar}`)}${quoteChar}`;
}
function isTrue(value: string) {
return TRUE_VALUES.includes(value.toLowerCase());
}
function isFalse(value: string) {
return FALSE_VALUES.includes(value.toLowerCase());
}
function isNumeric(value: string) {
return !isNaN(value as unknown as number) && !isNaN(parseFloat(value));
}
@ -274,7 +263,6 @@ export const stringExtensions: ExtensionMap = {
removeMarkdown,
sayHi,
stripTags,
toBoolean: isTrue,
toDate,
toDecimalNumber: toFloat,
toFloat,
@ -290,9 +278,6 @@ export const stringExtensions: ExtensionMap = {
length,
isDomain,
isEmail,
isTrue,
isFalse,
isNotTrue: isFalse,
isNumeric,
isUrl,
isURL: isUrl,

View File

@ -6,10 +6,6 @@ import { evaluate } from './Helpers';
describe('Data Transformation Functions', () => {
describe('Array Data Transformation Functions', () => {
test('.random() should work correctly on an array', () => {
expect(evaluate('={{ [1,2,3].random() }}')).not.toBeUndefined();
});
test('.randomItem() alias should work correctly on an array', () => {
expect(evaluate('={{ [1,2,3].randomItem() }}')).not.toBeUndefined();
});
@ -74,12 +70,6 @@ describe('Data Transformation Functions', () => {
expect(evaluate('={{ ["repeat","repeat","a","b","c"].first() }}')).toEqual('repeat');
});
test('.filter() should work correctly on an array', () => {
expect(evaluate('={{ ["repeat","repeat","a","b","c"].filter("repeat") }}')).toEqual(
expect.arrayContaining(['repeat', 'repeat']),
);
});
test('.merge() should work correctly on an array', () => {
expect(
evaluate(
@ -181,18 +171,5 @@ describe('Data Transformation Functions', () => {
[16, 17, 18, 19, 20],
]);
});
test('.filter() should work on a list of strings', () => {
expect(
evaluate(
'={{ ["i am a test string", "i should be kept", "i should be removed test"].filter("test", "remove") }}',
),
).toEqual(['i should be kept']);
expect(
evaluate(
'={{ ["i am a test string", "i should be kept test", "i should be removed"].filter("test") }}',
),
).toEqual(['i am a test string', 'i should be kept test']);
});
});
});

View File

@ -14,11 +14,6 @@ describe('Data Transformation Functions', () => {
);
});
test('.toTimeFromNow() should work correctly on a date', () => {
const JUST_NOW_STRING_RESULT = 'just now';
expect(evaluate('={{DateTime.now().toTimeFromNow()}}')).toEqual(JUST_NOW_STRING_RESULT);
});
test('.beginningOf("week") should work correctly on a date', () => {
expect(evaluate('={{(new Date).beginningOf("week")}}')).toEqual(
dateExtensions.functions.beginningOf(new Date(), ['week']),

View File

@ -7,14 +7,6 @@ import { evaluate } from './Helpers';
describe('Data Transformation Functions', () => {
describe('Number Data Transformation Functions', () => {
test('.random() should work correctly on a number', () => {
expect(evaluate('={{ Number(100).random() }}')).not.toBeUndefined();
});
test('.isBlank() should work correctly on a number', () => {
expect(evaluate('={{ Number(100).isBlank() }}')).toEqual(false);
});
test('.isPresent() should work correctly on a number', () => {
expect(evaluate('={{ Number(100).isPresent() }}')).toEqual(
numberExtensions.functions.isPresent(100),
@ -48,18 +40,6 @@ describe('Data Transformation Functions', () => {
expect(evaluate('={{ (NaN).round(3) }}')).toBeNaN();
});
test('.isTrue() should work on a number', () => {
expect(evaluate('={{ (1).isTrue() }}')).toEqual(true);
expect(evaluate('={{ (0).isTrue() }}')).toEqual(false);
expect(evaluate('={{ (NaN).isTrue() }}')).toEqual(false);
});
test('.isFalse() should work on a number', () => {
expect(evaluate('={{ (1).isFalse() }}')).toEqual(false);
expect(evaluate('={{ (0).isFalse() }}')).toEqual(true);
expect(evaluate('={{ (NaN).isFalse() }}')).toEqual(false);
});
test('.isOdd() should work on a number', () => {
expect(evaluate('={{ (9).isOdd() }}')).toEqual(true);
expect(evaluate('={{ (8).isOdd() }}')).toEqual(false);

View File

@ -92,48 +92,6 @@ describe('Data Transformation Functions', () => {
);
});
test('.toBoolean should work correctly on a string', () => {
const validTrue = ['y', 'yes', 't', 'true', '1', 'YES'];
for (const v of validTrue) {
expect(evaluate(`={{ "${v}".toBoolean() }}`)).toEqual(true);
}
const validFalse = ['n', 'no', 'f', 'false', '0', 'NO'];
for (const v of validFalse) {
expect(evaluate(`={{ "${v}".toBoolean() }}`)).toEqual(false);
}
expect(evaluate('={{ "maybe".toBoolean() }}')).toEqual(false);
});
test('.isTrue should work correctly on a string', () => {
const validTrue = ['y', 'yes', 't', 'true', '1', 'YES'];
for (const v of validTrue) {
expect(evaluate(`={{ "${v}".isTrue() }}`)).toEqual(true);
}
const validFalse = ['n', 'no', 'f', 'false', '0', 'NO'];
for (const v of validFalse) {
expect(evaluate(`={{ "${v}".isTrue() }}`)).toEqual(false);
}
expect(evaluate('={{ "maybe".isTrue() }}')).toEqual(false);
});
test('.isFalse should work correctly on a string', () => {
const validTrue = ['y', 'yes', 't', 'true', '1', 'YES'];
for (const v of validTrue) {
expect(evaluate(`={{ "${v}".isFalse() }}`)).toEqual(false);
}
const validFalse = ['n', 'no', 'f', 'false', '0', 'NO'];
for (const v of validFalse) {
expect(evaluate(`={{ "${v}".isFalse() }}`)).toEqual(true);
}
expect(evaluate('={{ "maybe".isFalse() }}')).toEqual(false);
});
test('.toFloat should work correctly on a string', () => {
expect(evaluate('={{ "1.1".toFloat() }}')).toEqual(1.1);
expect(evaluate('={{ "1.1".toDecimalNumber() }}')).toEqual(1.1);