index.cjs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. const dataTypeA = Array.isArray(a) ? "array" : "object";
  44. const dataTypeB = Array.isArray(b) ? "array" : "object";
  45. if (dataTypeA !== dataTypeB) return false;
  46. const keysA = Object.keys(a);
  47. const keysB = Object.keys(b);
  48. if (keysA.length !== keysB.length) return false;
  49. const symbolsA = Object.getOwnPropertySymbols(a);
  50. const symbolsB = Object.getOwnPropertySymbols(b);
  51. if (symbolsA.length !== symbolsB.length) return false;
  52. let setForA = cached.get(a);
  53. if (setForA == null) {
  54. setForA = /* @__PURE__ */ new Set();
  55. cached.set(a, setForA);
  56. } else if (setForA.has(b)) {
  57. return true;
  58. }
  59. setForA.add(b);
  60. let setForB = cached.get(b);
  61. if (setForB == null) {
  62. setForB = /* @__PURE__ */ new Set();
  63. cached.set(b, setForB);
  64. } else if (setForB.has(a)) {
  65. return true;
  66. }
  67. setForB.add(a);
  68. const propertyNamesA = [...keysA, ...symbolsA];
  69. for (const propertyNameA of propertyNamesA) {
  70. if (!Object.prototype.hasOwnProperty.call(b, propertyNameA)) return false;
  71. const propertyValueA = a[propertyNameA];
  72. const propertyValueB = b[propertyNameA];
  73. if (!compare(propertyValueA, propertyValueB)) return false;
  74. }
  75. return true;
  76. }, "compare");
  77. return compare(firstValue, secondValue);
  78. }
  79. __name(isDeepEqual, "isDeepEqual");
  80. // src/utils/get-data-type-of.js
  81. function getDataTypeOf(value) {
  82. if (typeof value === "string") {
  83. return DataType.STRING;
  84. } else if (typeof value === "number") {
  85. return DataType.NUMBER;
  86. } else if (typeof value === "boolean") {
  87. return DataType.BOOLEAN;
  88. } else if (Array.isArray(value)) {
  89. return DataType.ARRAY;
  90. } else if (typeof value === "object" && value !== null) {
  91. return DataType.OBJECT;
  92. }
  93. return DataType.ANY;
  94. }
  95. __name(getDataTypeOf, "getDataTypeOf");
  96. // src/empty-values-service.js
  97. var _EmptyValuesService = class _EmptyValuesService extends import_js_service.Service {
  98. /**
  99. * Empty values map.
  100. *
  101. * @type {Map<string, *[]>}
  102. */
  103. _emptyValuesMap = /* @__PURE__ */ new Map([
  104. [DataType.ANY, [void 0, null]],
  105. [DataType.STRING, [void 0, null, ""]],
  106. [DataType.NUMBER, [void 0, null, 0]],
  107. [DataType.BOOLEAN, [void 0, null]],
  108. [DataType.ARRAY, [void 0, null, []]],
  109. [DataType.OBJECT, [void 0, null, {}]]
  110. ]);
  111. /**
  112. * Set empty values of data type.
  113. *
  114. * @param {string} dataType
  115. * @param {*[]} emptyValues
  116. * @returns {EmptyValuesService}
  117. */
  118. setEmptyValuesOf(dataType, emptyValues) {
  119. if (!Object.values(DataType).includes(dataType))
  120. throw new import_js_format.Errorf(
  121. 'The argument "dataType" of the EmptyValuesService.setEmptyValuesOf must be one of data types: %l, but %v given.',
  122. Object.values(DataType),
  123. dataType
  124. );
  125. if (!Array.isArray(emptyValues))
  126. throw new import_js_format.Errorf(
  127. 'The argument "emptyValues" of the EmptyValuesService.setEmptyValuesOf must be an Array, but %v given.',
  128. emptyValues
  129. );
  130. this._emptyValuesMap.set(dataType, emptyValues);
  131. return this;
  132. }
  133. /**
  134. * Is empty.
  135. *
  136. * @param {*} value
  137. * @returns {boolean}
  138. */
  139. isEmpty(value) {
  140. const dataType = getDataTypeOf(value);
  141. return this._emptyValuesMap.get(dataType).some((v) => isDeepEqual(v, value));
  142. }
  143. /**
  144. * Is empty for type.
  145. *
  146. * @param {string} dataType
  147. * @param {*} value
  148. * @returns {boolean}
  149. */
  150. isEmptyByType(dataType, value) {
  151. if (!Object.values(DataType).includes(dataType))
  152. throw new import_js_format.Errorf(
  153. 'The argument "dataType" of EmptyValuesService.isEmptyByType must be one of data types: %l, but %v given.',
  154. Object.values(DataType),
  155. dataType
  156. );
  157. return this._emptyValuesMap.get(dataType).some((v) => isDeepEqual(v, value));
  158. }
  159. };
  160. __name(_EmptyValuesService, "EmptyValuesService");
  161. var EmptyValuesService = _EmptyValuesService;
  162. // Annotate the CommonJS export names for ESM import in node:
  163. 0 && (module.exports = {
  164. EmptyValuesService
  165. });