expression-language
Version:
Javascript implementation of symfony/expression-language
75 lines (74 loc) • 4.59 kB
JavaScript
;
var _dayjs = _interopRequireDefault(require("dayjs"));
var _defaultCustomFunctions = _interopRequireWildcard(require("../defaultCustomFunctions"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
test('isString', () => {
expect((0, _defaultCustomFunctions.isString)("hello")).toBe(true);
expect((0, _defaultCustomFunctions.isString)("")).toBe(true);
expect((0, _defaultCustomFunctions.isString)(123)).toBe(false);
expect((0, _defaultCustomFunctions.isString)(null)).toBe(false);
expect((0, _defaultCustomFunctions.isString)(undefined)).toBe(false);
expect((0, _defaultCustomFunctions.isString)({})).toBe(false);
});
test('strLen', () => {
expect((0, _defaultCustomFunctions.strLen)("hello")).toBe(5);
expect((0, _defaultCustomFunctions.strLen)("")).toBe(0);
expect((0, _defaultCustomFunctions.strLen)(12345)).toBe(0);
expect((0, _defaultCustomFunctions.strLen)(null)).toBe(0);
});
test('isEmail', () => {
expect((0, _defaultCustomFunctions.isEmail)("foo@example.com")).toBe(true);
expect((0, _defaultCustomFunctions.isEmail)("not-an-email")).toBe(false);
expect((0, _defaultCustomFunctions.isEmail)("missing@domain")).toBe(false);
expect((0, _defaultCustomFunctions.isEmail)(12345)).toBe(false);
expect((0, _defaultCustomFunctions.isEmail)(null)).toBe(false);
});
test('isPhone', () => {
expect((0, _defaultCustomFunctions.isPhone)("5551234567")).toBe(true);
expect((0, _defaultCustomFunctions.isPhone)("(555) 123-4567")).toBe(true);
expect((0, _defaultCustomFunctions.isPhone)("+15551234567")).toBe(true);
expect((0, _defaultCustomFunctions.isPhone)("555123")).toBe(false);
expect((0, _defaultCustomFunctions.isPhone)(5551234567)).toBe(false);
});
test('isNull', () => {
expect((0, _defaultCustomFunctions.isNull)(null)).toBe(true);
expect((0, _defaultCustomFunctions.isNull)(undefined)).toBe(false);
expect((0, _defaultCustomFunctions.isNull)(0)).toBe(false);
expect((0, _defaultCustomFunctions.isNull)("")).toBe(false);
});
test('isCurrency', () => {
expect((0, _defaultCustomFunctions.isCurrency)("$1,234.56")).toBe(true);
expect((0, _defaultCustomFunctions.isCurrency)("100")).toBe(true);
expect((0, _defaultCustomFunctions.isCurrency)("1234")).toBe(true);
expect((0, _defaultCustomFunctions.isCurrency)("abc")).toBe(false);
});
test('now returns a dayjs instance', () => {
expect(_dayjs.default.isDayjs((0, _defaultCustomFunctions.now)())).toBe(true);
});
test('dateFormat formats a dayjs instance', () => {
expect((0, _defaultCustomFunctions.dateFormat)((0, _dayjs.default)("2026-07-07"), "YYYY-MM-DD")).toBe("2026-07-07");
});
test('year extracts the 4-digit year', () => {
expect((0, _defaultCustomFunctions.year)((0, _dayjs.default)("2026-07-07"))).toBe("2026");
});
test('date extracts YYYY-MM-DD', () => {
expect((0, _defaultCustomFunctions.date)((0, _dayjs.default)("2026-07-07"))).toBe("2026-07-07");
});
test('string converts values to their string representation', () => {
expect((0, _defaultCustomFunctions.string)(123)).toBe("123");
expect((0, _defaultCustomFunctions.string)(true)).toBe("true");
expect((0, _defaultCustomFunctions.string)("already a string")).toBe("already a string");
});
test('string returns an empty string for null/undefined', () => {
expect((0, _defaultCustomFunctions.string)(null)).toBe("");
expect((0, _defaultCustomFunctions.string)(undefined)).toBe("");
});
test('int parses numeric strings and returns NaN for non-numeric input', () => {
expect((0, _defaultCustomFunctions.int)("42")).toBe(42);
expect((0, _defaultCustomFunctions.int)("42.9")).toBe(42);
expect((0, _defaultCustomFunctions.int)("not a number")).toBeNaN();
});
test('defaultCustomFunctions exposes all helpers by name', () => {
expect(Object.keys(_defaultCustomFunctions.default).sort()).toEqual(["date", "dateFormat", "int", "isCurrency", "isEmail", "isNull", "isPhone", "isString", "now", "string", "strLen", "year"].sort());
});