rich-domain
Version:
This package provide utils file and interfaces to assistant build a complex application with domain driving design
202 lines • 9.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Utils = void 0;
const validator_1 = require("./validator");
const remove_spaces_util_1 = require("./remove-spaces.util");
const remove_chars_util_1 = require("./remove-chars.util");
const replace_char_util_1 = require("./replace-char.util");
const multiply_number_util_1 = require("./multiply-number.util");
const divide_number_util_1 = require("./divide-number.util");
const subtract_number_util_1 = require("./subtract-number.util");
const sum_number_util_1 = require("./sum-number.util");
const increment_time_util_1 = require("./increment-time.util");
const decrement_time_util_1 = require("./decrement-time.util");
/**
* @description Utility class providing various helper methods for date, number, and string manipulations.
*/
class Utils {
static instance;
static validator = validator_1.default;
/**
* @description Creates a singleton instance of the Utils class.
* @returns {Utils} An instance of the Utils class.
*/
static create() {
if (!Utils.instance) {
Utils.instance = new Utils();
}
return Utils.instance;
}
/**
* @description Provides date manipulation utilities for adding or removing units of time.
* @param target The date to manipulate.
* @returns An object with methods for adding or removing units of time (days, hours, etc.).
*/
date(target) {
return ({
add: (value) => ({
/**
* @description add days to a date.
* @returns a new date with incremented days.
*/
days: () => new Date((0, increment_time_util_1.default)(target, value, 'day')),
/**
* @description add hours to a date.
* @returns a new date with incremented hours.
*/
hours: () => new Date((0, increment_time_util_1.default)(target, value, 'hour')),
/**
* @description add minutes to a date.
* @returns a new date with incremented minutes.
*/
minutes: () => new Date((0, increment_time_util_1.default)(target, value, 'minute')),
/**
* @description add weeks to a date.
* @returns a new date with incremented weeks.
*/
weeks: () => new Date((0, increment_time_util_1.default)(target, value, 'week')),
/**
* @description add months to a date.
* @returns a new date with incremented months.
*/
months: () => new Date((0, increment_time_util_1.default)(target, value, 'month')),
/**
* @description add years to a date.
* @returns a new date with incremented years.
*/
years: () => new Date((0, increment_time_util_1.default)(target, value, 'year'))
}),
remove: (value) => ({
/**
* @description remove days from a date.
* @returns a new date with removed days.
*/
days: () => new Date((0, decrement_time_util_1.default)(target, value, 'day')),
/**
* @description remove hours from a date.
* @returns a new date with removed hours.
*/
hours: () => new Date((0, decrement_time_util_1.default)(target, value, 'hour')),
/**
* @description remove minutes from a date.
* @returns a new date with removed minutes.
*/
minutes: () => new Date((0, decrement_time_util_1.default)(target, value, 'minute')),
/**
* @description remove weeks from a date.
* @returns a new date with removed weeks.
*/
weeks: () => new Date((0, decrement_time_util_1.default)(target, value, 'week')),
/**
* @description remove months from a date.
* @returns a new date with removed months.
*/
months: () => new Date((0, decrement_time_util_1.default)(target, value, 'month')),
/**
* @description remove years from a date.
* @returns a new date with removed years.
*/
years: () => new Date((0, decrement_time_util_1.default)(target, value, 'year'))
})
});
}
/**
* @description Provides number manipulation utilities, including mathematical operations.
* @param target The number to manipulate.
* @returns An object with methods for multiplication, division, subtraction, and addition.
*/
number(target) {
return {
/**
* @description multiply a value for another one.
* @param value number or string (number)
* @param options as object with fractionDigits option
* @default fractionDigits 5
* @returns result as number
* @sumary If you provide a string NAN (not a number) 0 will be considered as value.
*/
multiplyBy: (value, opt) => (0, multiply_number_util_1.default)(target, value, opt?.fractionDigits),
/**
* @description divide a value for another one.
* @param value number or string
* @param options as object with fractionDigits option
* @default fractionDigits 5
* @returns result as number
* @sumary If you provide a string NAN (not a number) 0 will be considered as value.
*/
divideBy: (value, opt) => (0, divide_number_util_1.default)(target, value, opt?.fractionDigits),
/**
* @description subtract a value for another one.
* @param value number or string
* @param options as object with fractionDigits option
* @default fractionDigits 5
* @returns result as number
* @sumary If you provide a string NAN (not a number) 0 will be considered as value.
*/
subtract: (value, opt) => (0, subtract_number_util_1.default)(target, value, opt?.fractionDigits),
/**
* @description sum a value with another one.
* @param value number or string
* @param options as object with fractionDigits option
* @default fractionDigits 5
* @returns result as number
* @sumary If you provide a string NAN (not a number) 0 will be considered as value.
*/
sum: (value, opt) => (0, sum_number_util_1.default)(target, value, opt?.fractionDigits)
};
}
/**
* @description Provides string manipulation utilities, including removing or replacing characters.
* @param target The string to manipulate.
* @returns An object with methods for character removal, space removal, and replacement.
*/
string(target) {
return {
/**
* @description remove all special chars from a string.
* @returns string with no special chars.
* @summary special chars: based on ascii table
* @example
* (asciiCode >= 33 && asciiCode <= 47) ||
* (asciiCode >= 58 && asciiCode <= 64) ||
* (asciiCode >= 91 && asciiCode <= 96) ||
* (asciiCode >= 123 && asciiCode <= 126)
*/
removeSpecialChars: () => (0, remove_chars_util_1.default)(target, (char) => Utils.validator.string(char).isSpecialChar()),
/**
* @description remove all spaces from text.
* @returns string with no spaces
*/
removeSpaces: () => (0, remove_spaces_util_1.default)(target),
/**
* @description remove all numbers from a text
* @returns string with no numbers.
*/
removeNumbers: () => (0, remove_chars_util_1.default)(target, (char) => Utils.validator.string(char).hasOnlyNumbers()),
/**
* @description remove only specific character from a text.
* @param char character to be removed.
* @returns string without char.
* @memberof replace. If you need to remove a word or more than one character from a text use replace function.
* @see replace
*/
removeChar: (char) => (0, remove_chars_util_1.default)(target, (val) => val === char),
/**
* @description replace any character or word from an string for some provided value.
* @param char character or word to be replaced.
* @param value character or word to be aplied.
* @returns text with replaced value.
* @example
* string("hello world").replace("world").to("dear");
*/
replace: (char) => ({ to: (value) => (0, replace_char_util_1.default)(target, char, value) })
};
}
constructor() {
Utils.validator = validator_1.default;
}
}
exports.Utils = Utils;
;
exports.default = Utils.create();
//# sourceMappingURL=util.js.map