contain.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. compare = compare || internals.compare(options);
  83. for (const [key, existing] of map.entries()) {
  84. if (compare(key, item)) {
  85. match = existing;
  86. break;
  87. }
  88. }
  89. }
  90. if (match) {
  91. ++match.hits;
  92. ++hits;
  93. if (options.once &&
  94. match.hits > match.allowed) {
  95. return false;
  96. }
  97. }
  98. }
  99. // Validate results
  100. if (options.only &&
  101. hits !== ref.length) {
  102. return false;
  103. }
  104. for (const match of map.values()) {
  105. if (match.hits === match.allowed) {
  106. continue;
  107. }
  108. if (match.hits < match.allowed &&
  109. !options.part) {
  110. return false;
  111. }
  112. }
  113. return !!hits;
  114. };
  115. internals.object = function (ref, values, options) {
  116. Assert(options.once === undefined, 'Cannot use option once with object');
  117. const keys = Utils.keys(ref, options);
  118. if (!keys.length) {
  119. return false;
  120. }
  121. // Keys list
  122. if (Array.isArray(values)) {
  123. return internals.array(keys, values, options);
  124. }
  125. // Key value pairs
  126. const symbols = Object.getOwnPropertySymbols(values).filter((sym) => values.propertyIsEnumerable(sym));
  127. const targets = [...Object.keys(values), ...symbols];
  128. const compare = internals.compare(options);
  129. const set = new Set(targets);
  130. for (const key of keys) {
  131. if (!set.has(key)) {
  132. if (options.only) {
  133. return false;
  134. }
  135. continue;
  136. }
  137. if (!compare(values[key], ref[key])) {
  138. return false;
  139. }
  140. set.delete(key);
  141. }
  142. if (set.size) {
  143. return options.part ? set.size < targets.length : false;
  144. }
  145. return true;
  146. };
  147. internals.string = function (ref, values, options) {
  148. // Empty string
  149. if (ref === '') {
  150. return values.length === 1 && values[0] === '' || // '' contains ''
  151. !options.once && !values.some((v) => v !== ''); // '' contains multiple '' if !once
  152. }
  153. // Map values
  154. const map = new Map();
  155. const patterns = [];
  156. for (const value of values) {
  157. Assert(typeof value === 'string', 'Cannot compare string reference to non-string value');
  158. if (value) {
  159. const existing = map.get(value);
  160. if (existing) {
  161. ++existing.allowed;
  162. }
  163. else {
  164. map.set(value, { allowed: 1, hits: 0 });
  165. patterns.push(EscapeRegex(value));
  166. }
  167. }
  168. else if (options.once ||
  169. options.only) {
  170. return false;
  171. }
  172. }
  173. if (!patterns.length) { // Non-empty string contains unlimited empty string
  174. return true;
  175. }
  176. // Match patterns
  177. const regex = new RegExp(`(${patterns.join('|')})`, 'g');
  178. const leftovers = ref.replace(regex, ($0, $1) => {
  179. ++map.get($1).hits;
  180. return ''; // Remove from string
  181. });
  182. // Validate results
  183. if (options.only &&
  184. leftovers) {
  185. return false;
  186. }
  187. let any = false;
  188. for (const match of map.values()) {
  189. if (match.hits) {
  190. any = true;
  191. }
  192. if (match.hits === match.allowed) {
  193. continue;
  194. }
  195. if (match.hits < match.allowed &&
  196. !options.part) {
  197. return false;
  198. }
  199. // match.hits > match.allowed
  200. if (options.once) {
  201. return false;
  202. }
  203. }
  204. return !!any;
  205. };
  206. internals.compare = function (options) {
  207. if (!options.deep) {
  208. return internals.shallow;
  209. }
  210. const hasOnly = options.only !== undefined;
  211. const hasPart = options.part !== undefined;
  212. const flags = {
  213. prototype: hasOnly ? options.only : hasPart ? !options.part : false,
  214. part: hasOnly ? !options.only : hasPart ? options.part : false
  215. };
  216. return (a, b) => DeepEqual(a, b, flags);
  217. };
  218. internals.shallow = function (a, b) {
  219. return a === b;
  220. };