contain.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. 'use strict';
  2. const Assert = require('./assert');
  3. const DeepEqual = require('./deepEqual');
  4. const EscapeRegex = require('./escapeRegex');
  5. const Utils = require('./utils');
  6. const internals = {};
  7. module.exports = function (ref, values, options = {}) { // options: { deep, once, only, part, symbols }
  8. /*
  9. string -> string(s)
  10. array -> item(s)
  11. object -> key(s)
  12. object -> object (key:value)
  13. */
  14. if (typeof values !== 'object') {
  15. values = [values];
  16. }
  17. Assert(!Array.isArray(values) || values.length, 'Values array cannot be empty');
  18. // String
  19. if (typeof ref === 'string') {
  20. return internals.string(ref, values, options);
  21. }
  22. // Array
  23. if (Array.isArray(ref)) {
  24. return internals.array(ref, values, options);
  25. }
  26. // Object
  27. Assert(typeof ref === 'object', 'Reference must be string or an object');
  28. return internals.object(ref, values, options);
  29. };
  30. internals.array = function (ref, values, options) {
  31. if (!Array.isArray(values)) {
  32. values = [values];
  33. }
  34. if (!ref.length) {
  35. return false;
  36. }
  37. if (options.only &&
  38. options.once &&
  39. ref.length !== values.length) {
  40. return false;
  41. }
  42. let compare;
  43. // Map values
  44. const map = new Map();
  45. for (const value of values) {
  46. if (!options.deep ||
  47. !value ||
  48. typeof value !== 'object') {
  49. const existing = map.get(value);
  50. if (existing) {
  51. ++existing.allowed;
  52. }
  53. else {
  54. map.set(value, { allowed: 1, hits: 0 });
  55. }
  56. }
  57. else {
  58. compare = compare || internals.compare(options);
  59. let found = false;
  60. for (const [key, existing] of map.entries()) {
  61. if (compare(key, value)) {
  62. ++existing.allowed;
  63. found = true;
  64. break;
  65. }
  66. }
  67. if (!found) {
  68. map.set(value, { allowed: 1, hits: 0 });
  69. }
  70. }
  71. }
  72. // Lookup values
  73. let hits = 0;
  74. for (const item of ref) {
  75. let match;
  76. if (!options.deep ||
  77. !item ||
  78. typeof item !== 'object') {
  79. match = map.get(item);
  80. }
  81. else {
  82. for (const [key, existing] of map.entries()) {
  83. if (compare(key, item)) {
  84. match = existing;
  85. break;
  86. }
  87. }
  88. }
  89. if (match) {
  90. ++match.hits;
  91. ++hits;
  92. if (options.once &&
  93. match.hits > match.allowed) {
  94. return false;
  95. }
  96. }
  97. }
  98. // Validate results
  99. if (options.only &&
  100. hits !== ref.length) {
  101. return false;
  102. }
  103. for (const match of map.values()) {
  104. if (match.hits === match.allowed) {
  105. continue;
  106. }
  107. if (match.hits < match.allowed &&
  108. !options.part) {
  109. return false;
  110. }
  111. }
  112. return !!hits;
  113. };
  114. internals.object = function (ref, values, options) {
  115. Assert(options.once === undefined, 'Cannot use option once with object');
  116. const keys = Utils.keys(ref, options);
  117. if (!keys.length) {
  118. return false;
  119. }
  120. // Keys list
  121. if (Array.isArray(values)) {
  122. return internals.array(keys, values, options);
  123. }
  124. // Key value pairs
  125. const symbols = Object.getOwnPropertySymbols(values).filter((sym) => values.propertyIsEnumerable(sym));
  126. const targets = [...Object.keys(values), ...symbols];
  127. const compare = internals.compare(options);
  128. const set = new Set(targets);
  129. for (const key of keys) {
  130. if (!set.has(key)) {
  131. if (options.only) {
  132. return false;
  133. }
  134. continue;
  135. }
  136. if (!compare(values[key], ref[key])) {
  137. return false;
  138. }
  139. set.delete(key);
  140. }
  141. if (set.size) {
  142. return options.part ? set.size < targets.length : false;
  143. }
  144. return true;
  145. };
  146. internals.string = function (ref, values, options) {
  147. // Empty string
  148. if (ref === '') {
  149. return values.length === 1 && values[0] === '' || // '' contains ''
  150. !options.once && !values.some((v) => v !== ''); // '' contains multiple '' if !once
  151. }
  152. // Map values
  153. const map = new Map();
  154. const patterns = [];
  155. for (const value of values) {
  156. Assert(typeof value === 'string', 'Cannot compare string reference to non-string value');
  157. if (value) {
  158. const existing = map.get(value);
  159. if (existing) {
  160. ++existing.allowed;
  161. }
  162. else {
  163. map.set(value, { allowed: 1, hits: 0 });
  164. patterns.push(EscapeRegex(value));
  165. }
  166. }
  167. else if (options.once ||
  168. options.only) {
  169. return false;
  170. }
  171. }
  172. if (!patterns.length) { // Non-empty string contains unlimited empty string
  173. return true;
  174. }
  175. // Match patterns
  176. const regex = new RegExp(`(${patterns.join('|')})`, 'g');
  177. const leftovers = ref.replace(regex, ($0, $1) => {
  178. ++map.get($1).hits;
  179. return ''; // Remove from string
  180. });
  181. // Validate results
  182. if (options.only &&
  183. leftovers) {
  184. return false;
  185. }
  186. let any = false;
  187. for (const match of map.values()) {
  188. if (match.hits) {
  189. any = true;
  190. }
  191. if (match.hits === match.allowed) {
  192. continue;
  193. }
  194. if (match.hits < match.allowed &&
  195. !options.part) {
  196. return false;
  197. }
  198. // match.hits > match.allowed
  199. if (options.once) {
  200. return false;
  201. }
  202. }
  203. return !!any;
  204. };
  205. internals.compare = function (options) {
  206. if (!options.deep) {
  207. return internals.shallow;
  208. }
  209. const hasOnly = options.only !== undefined;
  210. const hasPart = options.part !== undefined;
  211. const flags = {
  212. prototype: hasOnly ? options.only : hasPart ? !options.part : false,
  213. part: hasOnly ? !options.only : hasPart ? options.part : false
  214. };
  215. return (a, b) => DeepEqual(a, b, flags);
  216. };
  217. internals.shallow = function (a, b) {
  218. return a === b;
  219. };