Socket
Socket
Sign inDemoInstall

@briza/wegood

Package Overview
Dependencies
0
Maintainers
2
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.6 to 1.0.7

types/common/time.d.ts

4

changelog.md
# wegood changelog
## 1.0.7
* Feature: added new validation rule "year".
## 1.0.6

@@ -4,0 +8,0 @@

416

lib/wegood.esm.js

@@ -54,2 +54,74 @@ function _typeof(obj) {

function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(n);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
return arr2;
}
function _createForOfIteratorHelper(o) {
if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
if (Array.isArray(o) || (o = _unsupportedIterableToArray(o))) {
var i = 0;
var F = function () {};
return {
s: F,
n: function () {
if (i >= o.length) return {
done: true
};
return {
done: false,
value: o[i++]
};
},
e: function (e) {
throw e;
},
f: F
};
}
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
var it,
normalCompletion = true,
didErr = false,
err;
return {
s: function () {
it = o[Symbol.iterator]();
},
n: function () {
var step = it.next();
normalCompletion = step.done;
return step;
},
e: function (e) {
didErr = true;
err = e;
},
f: function () {
try {
if (!normalCompletion && it.return != null) it.return();
} finally {
if (didErr) throw err;
}
}
};
}
/* eslint-disable @typescript-eslint/no-explicit-any */

@@ -59,4 +131,4 @@

* Is Null or Undefined predicate.
* @param {mixed} value Rested value.
* @return {boolean}
* @param value Rested value.
* @return
*/

@@ -68,4 +140,4 @@ function isNullOrUndefined(value) {

* Is number predicate.
* @param {mixed} value Tested value.
* @return {boolean}
* @param value Tested value.
* @return
*/

@@ -78,4 +150,4 @@

* Is string type predicate.
* @param {mixed} value
* @return {boolean}
* @param value
* @return
*/

@@ -88,4 +160,4 @@

* Is function type predicate.
* @param {mixed} value
* @return {boolean}
* @param value
* @return
*/

@@ -98,4 +170,4 @@

* Is date type predicate.
* @param {mixed} value
* @return {boolean}
* @param value
* @return
*/

@@ -108,4 +180,4 @@

* Is Object
* @param {mixed} value tested value
* @return {boolean} result of the test
* @param value tested value
* @return result of the test
*/

@@ -117,24 +189,12 @@

// or after the specific date.
var BoundaryType;
/**
* Get today date.
*/
(function (BoundaryType) {
BoundaryType[BoundaryType["Start"] = 0] = "Start";
BoundaryType[BoundaryType["End"] = 1] = "End";
})(BoundaryType || (BoundaryType = {}));
function getToday() {
* Get today date.
*/
function today() {
return new Date(new Date().setHours(0, 0, 0, 0));
}
/**
* Expose mock-able rule members.
*/
var exported = {
getToday: getToday
};
// or after the specific date.
var BoundaryType;
/**

@@ -159,6 +219,11 @@ * Get the day offset from the matched Rx pattern.

*
* @param {string[]} match Regular expression match array.
* @return {number} Offset in days.
* @param match Regular expression match array.
* @return Offset in days.
*/
(function (BoundaryType) {
BoundaryType[BoundaryType["Start"] = 0] = "Start";
BoundaryType[BoundaryType["End"] = 1] = "End";
})(BoundaryType || (BoundaryType = {}));
function dateOffset(match) {

@@ -185,5 +250,5 @@ var amount = parseInt(match[1]);

* Create a date boundary based on the boundary type.
* @param {string|number|null|undefined} filter Date filter.
* @param {number} dir Boundary direction, 0 start, 1 end.
* @return {function} Function accepting the relative
* @param filter Date filter.
* @param dir Boundary direction, 0 start, 1 end.
* @return Function accepting the relative
* date which should be used in the comparison procedure.

@@ -240,3 +305,3 @@ */

} else if (filter !== '0') {
} else if ("".concat(filter) !== '0') {
throw new Error("illegal \"".concat(filter, "\" date boundary"));

@@ -258,9 +323,9 @@ }

var today = exported.getToday();
var todayDate = today();
if (dir === BoundaryType.Start) {
return date.getTime() >= today.getTime() + offset * 86400000;
return date.getTime() >= todayDate.getTime() + offset * 86400000;
}
return date.getTime() <= today.getTime() + offset * 86400000;
return date.getTime() <= todayDate.getTime() + offset * 86400000;
};

@@ -271,3 +336,3 @@ }

* E.g. '-05:00'
* @return {string} ISO timezone offset string
* @return ISO timezone offset string
*/

@@ -293,18 +358,15 @@

* be used to perform custom string to date conversion.
* @param {string} errorMsg Error message.
* @param {Date|string|number|null|undefined} start Start date boundary.
* @param errorMsg Error message.
* @param start Start date boundary.
* If null or undefined, there is no start boundary.
* @param {Date|string|number|null|undefined} end End date boundary.
* @param end End date boundary.
* If null or undefined, there is no end boundary.
* @param {function} transform Optional custom transform function, to
* @param transform Optional custom transform function, to
* convert the testing value into date object.
* @return {ValidationRule} validation function, fn(value) => true|string,
* @return validation function, fn(value) => true|string,
* returns true when valid, error message otherwise.
*/
function date(errorMsg, start, end, transform) {
var startBoundary = dateBoundary(start, BoundaryType.Start);
var endBoundary = dateBoundary(end, BoundaryType.End); // No boundaries
// No boundaries
if (isNullOrUndefined(start) && isNullOrUndefined(end)) {

@@ -317,2 +379,4 @@ console.warn("the date validation rule without start and end has\n no validation effect, it will be always validated as true.");

var startBoundary = dateBoundary(start, BoundaryType.Start);
var endBoundary = dateBoundary(end, BoundaryType.End);
return function (value) {

@@ -355,2 +419,104 @@ if (isNullOrUndefined(value)) {

/**
* Create year boundary based on the boundary type.
* @param filter Year filter.
* @param dir Boundary direction, 0 start, 1 end.
* @return Function accepting the relative year which should be used
* in the comparison procedure.
*/
function yearBoundary(filter, dir) {
if (isNullOrUndefined(filter)) {
return function () {
return true;
};
}
var isStringValue = isString(filter); // Boundary offset in years
var offset; // Dynamic offset
var dynamicOffset;
if (isStringValue) {
dynamicOffset = filter.match(/^(-?[1-9]\d*)y$/);
}
if (dynamicOffset && dynamicOffset !== null) {
offset = parseInt(dynamicOffset[1]); // or -1,1 (in past, in future)
} else if (['-1', '1'].includes("".concat(filter))) {
offset = Infinity * parseInt(filter); // Start 1 = tomorrow
if (dir === BoundaryType.Start && offset > 0) {
offset = 1; // 1 year in future, dynamic offset.
} // End -1 = any date in past
if (dir === BoundaryType.End && offset < 0) {
offset = -1; // 1 year in past, dynamic offset.
} // Current year
} else if (filter === 0) {
offset = 0;
}
return function (year) {
var currentYear = today().getFullYear();
if (offset !== undefined) {
return dir === BoundaryType.Start ? year >= currentYear + offset : year <= currentYear + offset;
}
return dir === BoundaryType.Start ? year >= filter : year <= filter;
};
}
/**
* Year validation rule.
* @param errorMsg Error message.
* @param start Start year boundary.
* If null or undefined, there is no start boundary.
* @param end End year boundary.
* If null or undefined, there is no end boundary.
* @return validation function, fn(value) => true|string,
* returns true when valid, error message otherwise.
*/
function year(errorMsg, start, end) {
// No boundaries
if (isNullOrUndefined(start) && isNullOrUndefined(end)) {
console.warn('the year validation rule without start and end has ' + 'no validation effect, it will be always validated as true.');
return function () {
return true;
};
}
var startBoundary = yearBoundary(start, BoundaryType.Start);
var endBoundary = yearBoundary(end, BoundaryType.End);
return function (value) {
if (isNullOrUndefined(value)) {
return errorMsg;
} // Get the year
if (isDate(value)) {
value = value.getFullYear();
} else {
value = parseInt(value, 10);
}
if (value < 0) {
console.warn("the year validation rule, invalid \"".concat(value, "\" year value"));
return errorMsg;
} // Check the value boundaries
if (startBoundary(value) === false || endBoundary(value) === false) {
return errorMsg;
}
return true;
};
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any

@@ -360,6 +526,6 @@

* Equal validation rule.
* @param {string} errorMsg Error message.
* @param {any|EqualPredicate} arg Matcher object, or a custom equality
* @param errorMsg Error message.
* @param arg Matcher object, or a custom equality
* predicate function, fn(val) returning true|false (equal / not equal).
* @return {ValidationRule} validation function, fn(value) => true|string,
* @return validation function, fn(value) => true|string,
* returns true when valid, error message otherwise.

@@ -389,5 +555,5 @@ */

* Exclude validation rule.
* @param {string} errorMsg Error message.
* @param {string[]|number[]} exclusions List of exclusions.
* @return {ValidationRule} validation function, fn(value) => true|string,
* @param errorMsg Error message.
* @param exclusions List of exclusions.
* @return validation function, fn(value) => true|string,
* returns true when valid, error message otherwise.

@@ -417,5 +583,5 @@ */

* Include validation rule.
* @param {string} errorMsg Error message.
* @param {string[]|number[]} inclusions List of inclusions.
* @return {ValidationRule} validation function, fn(value) => true|string,
* @param errorMsg Error message.
* @param inclusions List of inclusions.
* @return validation function, fn(value) => true|string,
* returns true when valid, error message otherwise.

@@ -445,8 +611,8 @@ */

* Length validation rule.
* @param {string} errorMsg Error message.
* @param {null|undefined|number} min Minimal length. If null or undefined,
* @param errorMsg Error message.
* @param min Minimal length. If null or undefined,
* there is no min length.
* @param {null|undefined|number} max Maximal length. If null or undefined,
* @param max Maximal length. If null or undefined,
* there is no max length.
* @return {ValidationRule} validation function, fn(value) => true|string,
* @return validation function, fn(value) => true|string,
* returns true when valid, error message otherwise.

@@ -492,5 +658,5 @@ */

* Pattern validation rule.
* @param {string} errorMsg Error message.
* @param {RegExp} pattern Regular expression.
* @return {ValidationRule} validation function, fn(value) => true|string,
* @param errorMsg Error message.
* @param pattern Regular expression.
* @return validation function, fn(value) => true|string,
* returns true when valid, error message otherwise.

@@ -523,8 +689,8 @@ */

* Range validation rule.
* @param {string} errorMsg Error message.
* @param {null|undefined|number} min Minimal boundary. If null or undefined,
* @param errorMsg Error message.
* @param min Minimal boundary. If null or undefined,
* there is no min boundary.
* @param {null|undefined|number} max Maximal boundary. If null or undefined,
* @param max Maximal boundary. If null or undefined,
* there is no max boundary.
* @return {ValidationRule} validation function, fn(value) => true|string,
* @return validation function, fn(value) => true|string,
* returns true when valid, error message otherwise.

@@ -572,4 +738,4 @@ */

* Present validation rule.
* @param {string} errorMsg Error message.
* @return {ValidationRule} validation function, fn(value) => true|string,
* @param errorMsg Error message.
* @return validation function, fn(value) => true|string,
* returns true when valid, error message otherwise.

@@ -609,8 +775,6 @@ */

*/
var Validator =
/*#__PURE__*/
function () {
var Validator = /*#__PURE__*/function () {
/**
* @constructor
* @param {ValidationRule[]} rules Validation rules.
* @param rules Validation rules.
* FIFO order, i.e. the rules will be tested from top to bottom.

@@ -627,8 +791,7 @@ */

var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
var _iterator = _createForOfIteratorHelper(rules),
_step;
try {
for (var _iterator = rules[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var rule = _step.value;

@@ -641,14 +804,5 @@

} catch (err) {
_didIteratorError = true;
_iteratorError = err;
_iterator.e(err);
} finally {
try {
if (!_iteratorNormalCompletion && _iterator["return"] != null) {
_iterator["return"]();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
_iterator.f();
}

@@ -660,3 +814,3 @@

* Get the validator rules.
* @return {ValidationRule[]}
* @return
*/

@@ -672,7 +826,7 @@

* Otherwise the return value is an error message of the failed rule.
* @param {mixed} value Validated value.
* @param {boolean} firstErrorOnly Return only first error.
* @param value Validated value.
* @param firstErrorOnly Return only first error.
* Defaults to true.
* If set to false, it returns an array of errors, if any.
* @return {ValidationResult}
* @return
*/

@@ -685,8 +839,8 @@ value: function validate(value) {

};
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
var _iterator2 = _createForOfIteratorHelper(this._rules),
_step2;
try {
for (var _iterator2 = this._rules[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var rule = _step2.value;

@@ -705,14 +859,5 @@ var result = rule(value);

} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
_iterator2.e(err);
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2["return"] != null) {
_iterator2["return"]();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
_iterator2.f();
}

@@ -724,4 +869,4 @@

* Validity predicate against the value.
* @param {mixed} value Validated value.
* @return {boolean}
* @param value Validated value.
* @return
*/

@@ -732,8 +877,7 @@

value: function valid(value) {
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
var _iterator3 = _createForOfIteratorHelper(this._rules),
_step3;
try {
for (var _iterator3 = this._rules[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
var rule = _step3.value;

@@ -747,14 +891,5 @@ var result = rule(value);

} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
_iterator3.e(err);
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3["return"] != null) {
_iterator3["return"]();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
_iterator3.f();
}

@@ -767,6 +902,6 @@

* Otherwise it returns empty array.
* @param {mixed} value Validated value.
* @param {boolean} firstErrorOnly Return only first error.
* @param value Validated value.
* @param firstErrorOnly Return only first error.
* Defaults to false.
* @return {string[]}
* @return
*/

@@ -779,8 +914,8 @@

var errors = [];
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
var _iterator4 = _createForOfIteratorHelper(this._rules),
_step4;
try {
for (var _iterator4 = this._rules[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
var rule = _step4.value;

@@ -798,14 +933,5 @@ var result = rule(value);

} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
_iterator4.e(err);
} finally {
try {
if (!_iteratorNormalCompletion4 && _iterator4["return"] != null) {
_iterator4["return"]();
}
} finally {
if (_didIteratorError4) {
throw _iteratorError4;
}
}
_iterator4.f();
}

@@ -826,2 +952,2 @@

export default Validator;
export { Validator, date, equal, exclude, include, length, pattern, present, range };
export { Validator, date, equal, exclude, include, length, pattern, present, range, year };

@@ -58,2 +58,74 @@ 'use strict';

function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(n);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
return arr2;
}
function _createForOfIteratorHelper(o) {
if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
if (Array.isArray(o) || (o = _unsupportedIterableToArray(o))) {
var i = 0;
var F = function () {};
return {
s: F,
n: function () {
if (i >= o.length) return {
done: true
};
return {
done: false,
value: o[i++]
};
},
e: function (e) {
throw e;
},
f: F
};
}
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
var it,
normalCompletion = true,
didErr = false,
err;
return {
s: function () {
it = o[Symbol.iterator]();
},
n: function () {
var step = it.next();
normalCompletion = step.done;
return step;
},
e: function (e) {
didErr = true;
err = e;
},
f: function () {
try {
if (!normalCompletion && it.return != null) it.return();
} finally {
if (didErr) throw err;
}
}
};
}
/* eslint-disable @typescript-eslint/no-explicit-any */

@@ -63,4 +135,4 @@

* Is Null or Undefined predicate.
* @param {mixed} value Rested value.
* @return {boolean}
* @param value Rested value.
* @return
*/

@@ -72,4 +144,4 @@ function isNullOrUndefined(value) {

* Is number predicate.
* @param {mixed} value Tested value.
* @return {boolean}
* @param value Tested value.
* @return
*/

@@ -82,4 +154,4 @@

* Is string type predicate.
* @param {mixed} value
* @return {boolean}
* @param value
* @return
*/

@@ -92,4 +164,4 @@

* Is function type predicate.
* @param {mixed} value
* @return {boolean}
* @param value
* @return
*/

@@ -102,4 +174,4 @@

* Is date type predicate.
* @param {mixed} value
* @return {boolean}
* @param value
* @return
*/

@@ -112,4 +184,4 @@

* Is Object
* @param {mixed} value tested value
* @return {boolean} result of the test
* @param value tested value
* @return result of the test
*/

@@ -121,24 +193,12 @@

// or after the specific date.
var BoundaryType;
/**
* Get today date.
*/
(function (BoundaryType) {
BoundaryType[BoundaryType["Start"] = 0] = "Start";
BoundaryType[BoundaryType["End"] = 1] = "End";
})(BoundaryType || (BoundaryType = {}));
function getToday() {
* Get today date.
*/
function today() {
return new Date(new Date().setHours(0, 0, 0, 0));
}
/**
* Expose mock-able rule members.
*/
var exported = {
getToday: getToday
};
// or after the specific date.
var BoundaryType;
/**

@@ -163,6 +223,11 @@ * Get the day offset from the matched Rx pattern.

*
* @param {string[]} match Regular expression match array.
* @return {number} Offset in days.
* @param match Regular expression match array.
* @return Offset in days.
*/
(function (BoundaryType) {
BoundaryType[BoundaryType["Start"] = 0] = "Start";
BoundaryType[BoundaryType["End"] = 1] = "End";
})(BoundaryType || (BoundaryType = {}));
function dateOffset(match) {

@@ -189,5 +254,5 @@ var amount = parseInt(match[1]);

* Create a date boundary based on the boundary type.
* @param {string|number|null|undefined} filter Date filter.
* @param {number} dir Boundary direction, 0 start, 1 end.
* @return {function} Function accepting the relative
* @param filter Date filter.
* @param dir Boundary direction, 0 start, 1 end.
* @return Function accepting the relative
* date which should be used in the comparison procedure.

@@ -244,3 +309,3 @@ */

} else if (filter !== '0') {
} else if ("".concat(filter) !== '0') {
throw new Error("illegal \"".concat(filter, "\" date boundary"));

@@ -262,9 +327,9 @@ }

var today = exported.getToday();
var todayDate = today();
if (dir === BoundaryType.Start) {
return date.getTime() >= today.getTime() + offset * 86400000;
return date.getTime() >= todayDate.getTime() + offset * 86400000;
}
return date.getTime() <= today.getTime() + offset * 86400000;
return date.getTime() <= todayDate.getTime() + offset * 86400000;
};

@@ -275,3 +340,3 @@ }

* E.g. '-05:00'
* @return {string} ISO timezone offset string
* @return ISO timezone offset string
*/

@@ -297,18 +362,15 @@

* be used to perform custom string to date conversion.
* @param {string} errorMsg Error message.
* @param {Date|string|number|null|undefined} start Start date boundary.
* @param errorMsg Error message.
* @param start Start date boundary.
* If null or undefined, there is no start boundary.
* @param {Date|string|number|null|undefined} end End date boundary.
* @param end End date boundary.
* If null or undefined, there is no end boundary.
* @param {function} transform Optional custom transform function, to
* @param transform Optional custom transform function, to
* convert the testing value into date object.
* @return {ValidationRule} validation function, fn(value) => true|string,
* @return validation function, fn(value) => true|string,
* returns true when valid, error message otherwise.
*/
function date(errorMsg, start, end, transform) {
var startBoundary = dateBoundary(start, BoundaryType.Start);
var endBoundary = dateBoundary(end, BoundaryType.End); // No boundaries
// No boundaries
if (isNullOrUndefined(start) && isNullOrUndefined(end)) {

@@ -321,2 +383,4 @@ console.warn("the date validation rule without start and end has\n no validation effect, it will be always validated as true.");

var startBoundary = dateBoundary(start, BoundaryType.Start);
var endBoundary = dateBoundary(end, BoundaryType.End);
return function (value) {

@@ -359,2 +423,104 @@ if (isNullOrUndefined(value)) {

/**
* Create year boundary based on the boundary type.
* @param filter Year filter.
* @param dir Boundary direction, 0 start, 1 end.
* @return Function accepting the relative year which should be used
* in the comparison procedure.
*/
function yearBoundary(filter, dir) {
if (isNullOrUndefined(filter)) {
return function () {
return true;
};
}
var isStringValue = isString(filter); // Boundary offset in years
var offset; // Dynamic offset
var dynamicOffset;
if (isStringValue) {
dynamicOffset = filter.match(/^(-?[1-9]\d*)y$/);
}
if (dynamicOffset && dynamicOffset !== null) {
offset = parseInt(dynamicOffset[1]); // or -1,1 (in past, in future)
} else if (['-1', '1'].includes("".concat(filter))) {
offset = Infinity * parseInt(filter); // Start 1 = tomorrow
if (dir === BoundaryType.Start && offset > 0) {
offset = 1; // 1 year in future, dynamic offset.
} // End -1 = any date in past
if (dir === BoundaryType.End && offset < 0) {
offset = -1; // 1 year in past, dynamic offset.
} // Current year
} else if (filter === 0) {
offset = 0;
}
return function (year) {
var currentYear = today().getFullYear();
if (offset !== undefined) {
return dir === BoundaryType.Start ? year >= currentYear + offset : year <= currentYear + offset;
}
return dir === BoundaryType.Start ? year >= filter : year <= filter;
};
}
/**
* Year validation rule.
* @param errorMsg Error message.
* @param start Start year boundary.
* If null or undefined, there is no start boundary.
* @param end End year boundary.
* If null or undefined, there is no end boundary.
* @return validation function, fn(value) => true|string,
* returns true when valid, error message otherwise.
*/
function year(errorMsg, start, end) {
// No boundaries
if (isNullOrUndefined(start) && isNullOrUndefined(end)) {
console.warn('the year validation rule without start and end has ' + 'no validation effect, it will be always validated as true.');
return function () {
return true;
};
}
var startBoundary = yearBoundary(start, BoundaryType.Start);
var endBoundary = yearBoundary(end, BoundaryType.End);
return function (value) {
if (isNullOrUndefined(value)) {
return errorMsg;
} // Get the year
if (isDate(value)) {
value = value.getFullYear();
} else {
value = parseInt(value, 10);
}
if (value < 0) {
console.warn("the year validation rule, invalid \"".concat(value, "\" year value"));
return errorMsg;
} // Check the value boundaries
if (startBoundary(value) === false || endBoundary(value) === false) {
return errorMsg;
}
return true;
};
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any

@@ -364,6 +530,6 @@

* Equal validation rule.
* @param {string} errorMsg Error message.
* @param {any|EqualPredicate} arg Matcher object, or a custom equality
* @param errorMsg Error message.
* @param arg Matcher object, or a custom equality
* predicate function, fn(val) returning true|false (equal / not equal).
* @return {ValidationRule} validation function, fn(value) => true|string,
* @return validation function, fn(value) => true|string,
* returns true when valid, error message otherwise.

@@ -393,5 +559,5 @@ */

* Exclude validation rule.
* @param {string} errorMsg Error message.
* @param {string[]|number[]} exclusions List of exclusions.
* @return {ValidationRule} validation function, fn(value) => true|string,
* @param errorMsg Error message.
* @param exclusions List of exclusions.
* @return validation function, fn(value) => true|string,
* returns true when valid, error message otherwise.

@@ -421,5 +587,5 @@ */

* Include validation rule.
* @param {string} errorMsg Error message.
* @param {string[]|number[]} inclusions List of inclusions.
* @return {ValidationRule} validation function, fn(value) => true|string,
* @param errorMsg Error message.
* @param inclusions List of inclusions.
* @return validation function, fn(value) => true|string,
* returns true when valid, error message otherwise.

@@ -449,8 +615,8 @@ */

* Length validation rule.
* @param {string} errorMsg Error message.
* @param {null|undefined|number} min Minimal length. If null or undefined,
* @param errorMsg Error message.
* @param min Minimal length. If null or undefined,
* there is no min length.
* @param {null|undefined|number} max Maximal length. If null or undefined,
* @param max Maximal length. If null or undefined,
* there is no max length.
* @return {ValidationRule} validation function, fn(value) => true|string,
* @return validation function, fn(value) => true|string,
* returns true when valid, error message otherwise.

@@ -496,5 +662,5 @@ */

* Pattern validation rule.
* @param {string} errorMsg Error message.
* @param {RegExp} pattern Regular expression.
* @return {ValidationRule} validation function, fn(value) => true|string,
* @param errorMsg Error message.
* @param pattern Regular expression.
* @return validation function, fn(value) => true|string,
* returns true when valid, error message otherwise.

@@ -527,8 +693,8 @@ */

* Range validation rule.
* @param {string} errorMsg Error message.
* @param {null|undefined|number} min Minimal boundary. If null or undefined,
* @param errorMsg Error message.
* @param min Minimal boundary. If null or undefined,
* there is no min boundary.
* @param {null|undefined|number} max Maximal boundary. If null or undefined,
* @param max Maximal boundary. If null or undefined,
* there is no max boundary.
* @return {ValidationRule} validation function, fn(value) => true|string,
* @return validation function, fn(value) => true|string,
* returns true when valid, error message otherwise.

@@ -576,4 +742,4 @@ */

* Present validation rule.
* @param {string} errorMsg Error message.
* @return {ValidationRule} validation function, fn(value) => true|string,
* @param errorMsg Error message.
* @return validation function, fn(value) => true|string,
* returns true when valid, error message otherwise.

@@ -613,8 +779,6 @@ */

*/
var Validator =
/*#__PURE__*/
function () {
var Validator = /*#__PURE__*/function () {
/**
* @constructor
* @param {ValidationRule[]} rules Validation rules.
* @param rules Validation rules.
* FIFO order, i.e. the rules will be tested from top to bottom.

@@ -631,8 +795,7 @@ */

var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
var _iterator = _createForOfIteratorHelper(rules),
_step;
try {
for (var _iterator = rules[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var rule = _step.value;

@@ -645,14 +808,5 @@

} catch (err) {
_didIteratorError = true;
_iteratorError = err;
_iterator.e(err);
} finally {
try {
if (!_iteratorNormalCompletion && _iterator["return"] != null) {
_iterator["return"]();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
_iterator.f();
}

@@ -664,3 +818,3 @@

* Get the validator rules.
* @return {ValidationRule[]}
* @return
*/

@@ -676,7 +830,7 @@

* Otherwise the return value is an error message of the failed rule.
* @param {mixed} value Validated value.
* @param {boolean} firstErrorOnly Return only first error.
* @param value Validated value.
* @param firstErrorOnly Return only first error.
* Defaults to true.
* If set to false, it returns an array of errors, if any.
* @return {ValidationResult}
* @return
*/

@@ -689,8 +843,8 @@ value: function validate(value) {

};
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
var _iterator2 = _createForOfIteratorHelper(this._rules),
_step2;
try {
for (var _iterator2 = this._rules[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var rule = _step2.value;

@@ -709,14 +863,5 @@ var result = rule(value);

} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
_iterator2.e(err);
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2["return"] != null) {
_iterator2["return"]();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
_iterator2.f();
}

@@ -728,4 +873,4 @@

* Validity predicate against the value.
* @param {mixed} value Validated value.
* @return {boolean}
* @param value Validated value.
* @return
*/

@@ -736,8 +881,7 @@

value: function valid(value) {
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
var _iterator3 = _createForOfIteratorHelper(this._rules),
_step3;
try {
for (var _iterator3 = this._rules[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
var rule = _step3.value;

@@ -751,14 +895,5 @@ var result = rule(value);

} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
_iterator3.e(err);
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3["return"] != null) {
_iterator3["return"]();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
_iterator3.f();
}

@@ -771,6 +906,6 @@

* Otherwise it returns empty array.
* @param {mixed} value Validated value.
* @param {boolean} firstErrorOnly Return only first error.
* @param value Validated value.
* @param firstErrorOnly Return only first error.
* Defaults to false.
* @return {string[]}
* @return
*/

@@ -783,8 +918,8 @@

var errors = [];
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
var _iterator4 = _createForOfIteratorHelper(this._rules),
_step4;
try {
for (var _iterator4 = this._rules[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
var rule = _step4.value;

@@ -802,14 +937,5 @@ var result = rule(value);

} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
_iterator4.e(err);
} finally {
try {
if (!_iteratorNormalCompletion4 && _iterator4["return"] != null) {
_iterator4["return"]();
}
} finally {
if (_didIteratorError4) {
throw _iteratorError4;
}
}
_iterator4.f();
}

@@ -839,1 +965,2 @@

exports.range = range;
exports.year = year;
{
"name": "@briza/wegood",
"version": "1.0.6",
"version": "1.0.7",
"description": "Tiny validation library, so wegood with data.",

@@ -53,5 +53,6 @@ "main": "lib/wegood.js",

"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-simple-import-sort": "^5.0.2",
"eslint-plugin-standard": "^4.0.1",
"jest": "^24.9.0",
"rollup": "^1.24.0",
"rollup": "^2.3.3",
"rollup-plugin-babel": "^4.3.3",

@@ -62,6 +63,6 @@ "rollup-plugin-commonjs": "^10.1.0",

"ts-jest": "^24.1.0",
"typedoc": "^0.16.7",
"typescript": "^3.6.4"
"typedoc": "^0.17.4",
"typescript": "^3.8.3"
},
"dependencies": {}
}
# wegood
Tiny validation library, so wegood with data.
> Revision: January 29, 2020.
> Revision: April 13, 2020.

@@ -49,2 +49,5 @@ ## About

- [Date Tested Value Format](#date-tested-value-format)
- [Year](#year)
- [Relative Year Offset](#relative-year-offset)
- [Year Tested Value Format](#year-tested-value-format)
- [Custom Validation Rule](#custom-validation-rule)

@@ -441,4 +444,4 @@ - [Contributing](#contributing)

| errorMessage | Error message. | 'the date in not in valid range.' |
| start | Start date boundary - ```ISO date string``` (yyyy-mm-dd), ```Date object```, or [Relative Date Offset](#relative-date-offset). If set to ```undefined``` or ```null```, it is being ignored. | 3 |
| end | End date boundary - ```ISO date string``` (yyyy-mm-dd), ```Date object```, or [Relative Date Offset](#relative-date-offset). If set to ```undefined``` or ```null```, it is being ignored. | 3 |
| start | Start date boundary: ```ISO date string``` (yyyy-mm-dd), ```Date object```, or [Relative Date Offset](#relative-date-offset). If set to ```undefined``` or ```null```, it is being ignored. | 2020-03-16 |
| end | End date boundary: ```ISO date string``` (yyyy-mm-dd), ```Date object```, or [Relative Date Offset](#relative-date-offset). If set to ```undefined``` or ```null```, it is being ignored. | 3y |
| transform | Custom Date object transformer function. Optional. | ```(value) => new Date(value)``` |

@@ -449,3 +452,3 @@

#### Relative Date Offset
Instead of a concrete ISO date string or Date object below annotated shortcodes might be used to set the date boundary relative to TODAY date.
Instead of a concrete ISO date string or Date object below annotated shortcodes may be used to set the date boundary relative to TODAY date.

@@ -518,2 +521,66 @@ | Annotation | Meaning | Example |

### Year
Verify that the tested value is in the year range.
```js
import { year } from '@briza/wegood';
```
**Function Arguments**
```typescript
year(errorMessage, start, end)
```
| Argument | Notes | Example |
| --- | --- | --- |
| errorMessage | Error message. | 'the year in not in valid range.' |
| start | Start date boundary: ```4-digit year number``` or [Relative Year Offset](#relative-year-offset). If set to ```undefined``` or ```null```, it is being ignored. | 2000 |
| end | End date boundary: ```4-digit year number``` or [Relative Year Offset](#relative-year-offset). If set to ```undefined``` or ```null```, it is being ignored. | 3y |
> [Code documentation](https://briza-insurance.github.io/wegood/modules/_rule_year_.html).
#### Relative Year Offset
Instead of a concrete year below annotated shortcodes may be used to set the year boundary relative to the CURRENT year.
| Annotation | Meaning | Example |
| -- | -- | -- |
| 0 | Current year. | 0 |
| -1 | In past. | -1 |
| 1 | In future. | 1 |
| -Ny | N years in past, relative from current year. | -2y |
| Ny | N years in past, relative from current year. | 2y |
**Example**
```typescript
// The value (year) must between the given years range.
year('error message', 2000, 2020);
// The value (year) must after the year (inclusive).
year('error message', 2000);
// The value (year) must before the year (inclusive).
year('error message', undefined|null, 2020);
/**
* Relative offsets, relative to current year.
*/
// Any year in past, until current year.
year('error message', -1, 0);
// Any year in future, starts from current year.
year('error message', 0, 1);
// Any year between 2 years ago, up to 2 years from current year.
year('error message', '-2y', '2y');
// Combined fixed year with relative year.
// Any year from 2000 until current year.
year('error message', 2000, 0);
```
#### Year Tested Value Format
The tested value must be passed to the validation as non zero number (string or number), otherwise the validation will fail implicitly with warning message.
## Custom Validation Rule

@@ -520,0 +587,0 @@ Validation rule blueprint (typescript):

/**
* Is Null or Undefined predicate.
* @param {mixed} value Rested value.
* @return {boolean}
* @param value Rested value.
* @return
*/

@@ -9,16 +9,16 @@ export declare function isNullOrUndefined(value: any): boolean;

* Is number predicate.
* @param {mixed} value Tested value.
* @return {boolean}
* @param value Tested value.
* @return
*/
export declare function isNumber(value: any): boolean;
export declare function isNumber(value: any): value is number;
/**
* Is string type predicate.
* @param {mixed} value
* @return {boolean}
* @param value
* @return
*/
export declare function isString(value: any): boolean;
export declare function isString(value: any): value is string;
/**
* Is function type predicate.
* @param {mixed} value
* @return {boolean}
* @param value
* @return
*/

@@ -28,11 +28,11 @@ export declare function isFunction(value: any): boolean;

* Is date type predicate.
* @param {mixed} value
* @return {boolean}
* @param value
* @return
*/
export declare function isDate(value: any): boolean;
export declare function isDate(value: any): value is Date;
/**
* Is Object
* @param {mixed} value tested value
* @return {boolean} result of the test
* @param value tested value
* @return result of the test
*/
export declare function isObject(value: any): boolean;
import { ValidationRule } from './rule';
import date from './rule/date';
import year from './rule/year';
import equal from './rule/equal';

@@ -13,3 +14,3 @@ import exclude from './rule/exclude';

*/
export { present, date, equal, exclude, include, length, pattern, range };
export { present, date, year, equal, exclude, include, length, pattern, range };
/**

@@ -31,3 +32,3 @@ * Validation result.

* @constructor
* @param {ValidationRule[]} rules Validation rules.
* @param rules Validation rules.
* FIFO order, i.e. the rules will be tested from top to bottom.

@@ -38,3 +39,3 @@ */

* Get the validator rules.
* @return {ValidationRule[]}
* @return
*/

@@ -46,7 +47,7 @@ get rules(): ValidationRule[];

* Otherwise the return value is an error message of the failed rule.
* @param {mixed} value Validated value.
* @param {boolean} firstErrorOnly Return only first error.
* @param value Validated value.
* @param firstErrorOnly Return only first error.
* Defaults to true.
* If set to false, it returns an array of errors, if any.
* @return {ValidationResult}
* @return
*/

@@ -56,4 +57,4 @@ validate(value: any, firstErrorOnly?: boolean): ValidationResult;

* Validity predicate against the value.
* @param {mixed} value Validated value.
* @return {boolean}
* @param value Validated value.
* @return
*/

@@ -64,6 +65,6 @@ valid(value: any): boolean;

* Otherwise it returns empty array.
* @param {mixed} value Validated value.
* @param {boolean} firstErrorOnly Return only first error.
* @param value Validated value.
* @param firstErrorOnly Return only first error.
* Defaults to false.
* @return {string[]}
* @return
*/

@@ -70,0 +71,0 @@ errors(value: any, firstErrorOnly?: boolean): string[];

import { ValidationRule } from '.';
export declare enum BoundaryType {
Start = 0,
End = 1
}
/**
* Get today date.
*/
export declare function getToday(): Date;
/**
* Expose mock-able rule members.
* Get local timezone offset as ISO string
* E.g. '-05:00'
* @return ISO timezone offset string
*/
export declare const exported: {
getToday: typeof getToday;
};
export declare function getISOTimezoneOffset(): string;
/**

@@ -17,10 +17,10 @@ * Date validation rule.

* be used to perform custom string to date conversion.
* @param {string} errorMsg Error message.
* @param {Date|string|number|null|undefined} start Start date boundary.
* @param errorMsg Error message.
* @param start Start date boundary.
* If null or undefined, there is no start boundary.
* @param {Date|string|number|null|undefined} end End date boundary.
* @param end End date boundary.
* If null or undefined, there is no end boundary.
* @param {function} transform Optional custom transform function, to
* @param transform Optional custom transform function, to
* convert the testing value into date object.
* @return {ValidationRule} validation function, fn(value) => true|string,
* @return validation function, fn(value) => true|string,
* returns true when valid, error message otherwise.

@@ -27,0 +27,0 @@ */

@@ -5,6 +5,6 @@ import { ValidationRule } from '.';

* Equal validation rule.
* @param {string} errorMsg Error message.
* @param {any|EqualPredicate} arg Matcher object, or a custom equality
* @param errorMsg Error message.
* @param arg Matcher object, or a custom equality
* predicate function, fn(val) returning true|false (equal / not equal).
* @return {ValidationRule} validation function, fn(value) => true|string,
* @return validation function, fn(value) => true|string,
* returns true when valid, error message otherwise.

@@ -11,0 +11,0 @@ */

import { ValidationRule } from '.';
/**
* Exclude validation rule.
* @param {string} errorMsg Error message.
* @param {string[]|number[]} exclusions List of exclusions.
* @return {ValidationRule} validation function, fn(value) => true|string,
* @param errorMsg Error message.
* @param exclusions List of exclusions.
* @return validation function, fn(value) => true|string,
* returns true when valid, error message otherwise.

@@ -8,0 +8,0 @@ */

import { ValidationRule } from '.';
/**
* Include validation rule.
* @param {string} errorMsg Error message.
* @param {string[]|number[]} inclusions List of inclusions.
* @return {ValidationRule} validation function, fn(value) => true|string,
* @param errorMsg Error message.
* @param inclusions List of inclusions.
* @return validation function, fn(value) => true|string,
* returns true when valid, error message otherwise.

@@ -8,0 +8,0 @@ */

@@ -0,0 +0,0 @@ /**

import { ValidationRule } from '.';
/**
* Length validation rule.
* @param {string} errorMsg Error message.
* @param {null|undefined|number} min Minimal length. If null or undefined,
* @param errorMsg Error message.
* @param min Minimal length. If null or undefined,
* there is no min length.
* @param {null|undefined|number} max Maximal length. If null or undefined,
* @param max Maximal length. If null or undefined,
* there is no max length.
* @return {ValidationRule} validation function, fn(value) => true|string,
* @return validation function, fn(value) => true|string,
* returns true when valid, error message otherwise.

@@ -11,0 +11,0 @@ */

import { ValidationRule } from '.';
/**
* Pattern validation rule.
* @param {string} errorMsg Error message.
* @param {RegExp} pattern Regular expression.
* @return {ValidationRule} validation function, fn(value) => true|string,
* @param errorMsg Error message.
* @param pattern Regular expression.
* @return validation function, fn(value) => true|string,
* returns true when valid, error message otherwise.

@@ -8,0 +8,0 @@ */

import { ValidationRule } from '.';
/**
* Present validation rule.
* @param {string} errorMsg Error message.
* @return {ValidationRule} validation function, fn(value) => true|string,
* @param errorMsg Error message.
* @return validation function, fn(value) => true|string,
* returns true when valid, error message otherwise.

@@ -7,0 +7,0 @@ */

import { ValidationRule } from '.';
/**
* Range validation rule.
* @param {string} errorMsg Error message.
* @param {null|undefined|number} min Minimal boundary. If null or undefined,
* @param errorMsg Error message.
* @param min Minimal boundary. If null or undefined,
* there is no min boundary.
* @param {null|undefined|number} max Maximal boundary. If null or undefined,
* @param max Maximal boundary. If null or undefined,
* there is no max boundary.
* @return {ValidationRule} validation function, fn(value) => true|string,
* @return validation function, fn(value) => true|string,
* returns true when valid, error message otherwise.

@@ -11,0 +11,0 @@ */

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc