index.cjs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
  7. var __export = (target, all) => {
  8. for (var name in all)
  9. __defProp(target, name, { get: all[name], enumerable: true });
  10. };
  11. var __copyProps = (to, from, except, desc) => {
  12. if (from && typeof from === "object" || typeof from === "function") {
  13. for (let key of __getOwnPropNames(from))
  14. if (!__hasOwnProp.call(to, key) && key !== except)
  15. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  16. }
  17. return to;
  18. };
  19. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  20. // src/index.js
  21. var index_exports = {};
  22. __export(index_exports, {
  23. EmptyValuesService: () => EmptyValuesService
  24. });
  25. module.exports = __toCommonJS(index_exports);
  26. // src/data-type.js
  27. var DataType = {
  28. ANY: "any",
  29. STRING: "string",
  30. NUMBER: "number",
  31. BOOLEAN: "boolean",
  32. ARRAY: "array",
  33. OBJECT: "object"
  34. };
  35. // src/empty-values-service.js
  36. var import_js_format = require("@e22m4u/js-format");
  37. var import_js_service = require("@e22m4u/js-service");
  38. // src/utils/is-deep-equal.js
  39. function isDeepEqual(firstValue, secondValue) {
  40. const cached = /* @__PURE__ */ new WeakMap();
  41. const compare = /* @__PURE__ */ __name((a, b) => {
  42. if (a === null || b === null) return a === b;
  43. if (typeof a !== "object" || typeof b !== "object") return a === b;
  44. if (a.constructor !== b.constructor) return false;
  45. const dataTypeA = Array.isArray(a) ? "array" : "object";
  46. const dataTypeB = Array.isArray(b) ? "array" : "object";
  47. if (dataTypeA !== dataTypeB) return false;
  48. const keysA = Object.keys(a);
  49. const keysB = Object.keys(b);
  50. if (keysA.length !== keysB.length) return false;
  51. const symbolsA = Object.getOwnPropertySymbols(a);
  52. const symbolsB = Object.getOwnPropertySymbols(b);
  53. if (symbolsA.length !== symbolsB.length) return false;
  54. let setForA = cached.get(a);
  55. if (setForA == null) {
  56. setForA = /* @__PURE__ */ new Set();
  57. cached.set(a, setForA);
  58. } else if (setForA.has(b)) {
  59. return true;
  60. }
  61. setForA.add(b);
  62. let setForB = cached.get(b);
  63. if (setForB == null) {
  64. setForB = /* @__PURE__ */ new Set();
  65. cached.set(b, setForB);
  66. } else if (setForB.has(a)) {
  67. return true;
  68. }
  69. setForB.add(a);
  70. const propertyNamesA = [...keysA, ...symbolsA];
  71. for (const propertyNameA of propertyNamesA) {
  72. if (!Object.prototype.hasOwnProperty.call(b, propertyNameA)) return false;
  73. const propertyValueA = a[propertyNameA];
  74. const propertyValueB = b[propertyNameA];
  75. if (!compare(propertyValueA, propertyValueB)) return false;
  76. }
  77. return true;
  78. }, "compare");
  79. return compare(firstValue, secondValue);
  80. }
  81. __name(isDeepEqual, "isDeepEqual");
  82. // src/utils/get-data-type-of.js
  83. function getDataTypeOf(value) {
  84. if (typeof value === "string") {
  85. return DataType.STRING;
  86. } else if (typeof value === "number") {
  87. return DataType.NUMBER;
  88. } else if (typeof value === "boolean") {
  89. return DataType.BOOLEAN;
  90. } else if (Array.isArray(value)) {
  91. return DataType.ARRAY;
  92. } else if (typeof value === "object" && value !== null) {
  93. return DataType.OBJECT;
  94. }
  95. return DataType.ANY;
  96. }
  97. __name(getDataTypeOf, "getDataTypeOf");
  98. // src/empty-values-service.js
  99. var _EmptyValuesService = class _EmptyValuesService extends import_js_service.Service {
  100. /**
  101. * Empty values map.
  102. *
  103. * @type {Map<string, *[]>}
  104. */
  105. _emptyValuesMap = /* @__PURE__ */ new Map([
  106. [DataType.ANY, [void 0, null]],
  107. [DataType.STRING, [void 0, null, ""]],
  108. [DataType.NUMBER, [void 0, null, 0]],
  109. [DataType.BOOLEAN, [void 0, null]],
  110. [DataType.ARRAY, [void 0, null, []]],
  111. [DataType.OBJECT, [void 0, null, {}]]
  112. ]);
  113. /**
  114. * Set empty values of data type.
  115. *
  116. * @param {DataType} dataType
  117. * @param {*[]} emptyValues
  118. * @returns {EmptyValuesService}
  119. */
  120. setEmptyValuesOf(dataType, emptyValues) {
  121. if (!Object.values(DataType).includes(dataType))
  122. throw new import_js_format.Errorf(
  123. 'The argument "dataType" of the EmptyValuesService.setEmptyValuesOf must be one of data types: %l, but %v given.',
  124. Object.values(DataType),
  125. dataType
  126. );
  127. if (!Array.isArray(emptyValues))
  128. throw new import_js_format.Errorf(
  129. 'The argument "emptyValues" of the EmptyValuesService.setEmptyValuesOf must be an Array, but %v given.',
  130. emptyValues
  131. );
  132. this._emptyValuesMap.set(dataType, emptyValues);
  133. return this;
  134. }
  135. /**
  136. * Is empty.
  137. *
  138. * @param {*} value
  139. * @returns {boolean}
  140. */
  141. isEmpty(value) {
  142. const dataType = getDataTypeOf(value);
  143. return this._emptyValuesMap.get(dataType).some((v) => isDeepEqual(v, value));
  144. }
  145. /**
  146. * Is empty for type.
  147. *
  148. * @param {DataType} dataType
  149. * @param {*} value
  150. * @returns {boolean}
  151. */
  152. isEmptyByType(dataType, value) {
  153. if (!Object.values(DataType).includes(dataType))
  154. throw new import_js_format.Errorf(
  155. 'The argument "dataType" of EmptyValuesService.isEmptyByType must be one of data types: %l, but %v given.',
  156. Object.values(DataType),
  157. dataType
  158. );
  159. return this._emptyValuesMap.get(dataType).some((v) => isDeepEqual(v, value));
  160. }
  161. };
  162. __name(_EmptyValuesService, "EmptyValuesService");
  163. var EmptyValuesService = _EmptyValuesService;
  164. // Annotate the CommonJS export names for ESM import in node:
  165. 0 && (module.exports = {
  166. EmptyValuesService
  167. });