expression-language
Version:
Javascript implementation of symfony/expression-language
86 lines (85 loc) • 1.95 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.year = exports.string = exports.strLen = exports.now = exports.isString = exports.isPhone = exports.isNull = exports.isEmail = exports.isCurrency = exports.int = exports.default = exports.dateFormat = exports.date = void 0;
var _moment = _interopRequireDefault(require("moment"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const isString = s => {
return typeof s === "string";
};
exports.isString = isString;
const strLen = s => {
if (isString(s)) {
return s.length;
}
return 0;
};
exports.strLen = strLen;
const isEmail = s => {
if (isString(s)) {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(s);
}
return false;
};
exports.isEmail = isEmail;
const isPhone = s => {
if (isString(s)) {
if (s.substring(0, 2) === "+1") {
s = s.substring(2);
}
return /^\d{10}$/.test(s.replace(/\D/g, ""));
}
return false;
};
exports.isPhone = isPhone;
const isNull = s => {
return s === null;
};
exports.isNull = isNull;
const isCurrency = s => {
return /(?=.*?\d)^\$?(([1-9]\d{0,2}(,\d{3})*)|\d+)?(\.\d{1,2})?$/.test(s);
};
exports.isCurrency = isCurrency;
const now = () => {
return (0, _moment.default)();
};
exports.now = now;
const dateFormat = (m, format) => {
return m.format(format);
};
exports.dateFormat = dateFormat;
const year = m => {
return dateFormat(m, "YYYY");
};
exports.year = year;
const date = m => {
return dateFormat(m, "YYYY-MM-DD");
};
exports.date = date;
const string = s => {
if (s.toString !== undefined) {
return s.toString();
}
return "";
};
exports.string = string;
const int = s => {
return parseInt(s);
};
exports.int = int;
let defaultCustomFunctions = {
isString,
strLen,
isEmail,
isPhone,
isNull,
isCurrency,
now,
dateFormat,
year,
date,
string,
int
};
var _default = exports.default = defaultCustomFunctions;