index.cjs 5.6 KB

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