@legalplace/lplogic 2.3.2-staging.0 → 2.3.2-staging.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/LpLogic.js CHANGED
@@ -41,10 +41,25 @@ var parseNumber = function (value) {
41
41
  return parseFloat(value.replace(",", "."));
42
42
  return value;
43
43
  };
44
+ var cleanString = function (value) {
45
+ var raw = typeof value === "number" ? value.toString() : value.trim();
46
+ return raw
47
+ .normalize("NFD")
48
+ .replace(/[\u0300-\u036f]/g, "")
49
+ .replace(/œ/g, "oe")
50
+ .replace(/Œ/g, "oe")
51
+ .replace(/æ/g, "ae")
52
+ .replace(/Æ/g, "ae")
53
+ .toLowerCase();
54
+ };
44
55
  var safeEqual = function (value, comparator) {
45
56
  var parsedValue = typeof value === "number" ? value : parseNumber(value);
46
57
  var parsedComparator = typeof comparator === "number" ? comparator : parseNumber(comparator);
47
- return parsedValue === parsedComparator;
58
+ if (typeof parsedValue === "number" &&
59
+ typeof parsedComparator === "number") {
60
+ return parsedValue === parsedComparator;
61
+ }
62
+ return cleanString(value) === cleanString(comparator);
48
63
  };
49
64
  json_logic_js_1.default.add_operation("=", function (values, comparator, parents) {
50
65
  var reducedValues = reduceValues(values, parents, function (value, parent) {
@@ -130,13 +145,7 @@ json_logic_js_1.default.add_operation("has-many", function (values, comparator,
130
145
  });
131
146
  var is = function (values, comparator, parents) {
132
147
  var reducedValues = reduceValues(values, parents, function (value, parent) {
133
- var cleanValue = (typeof value === "number"
134
- ? value.toString()
135
- : value.trim()).toLowerCase();
136
- var cleanComparator = (typeof comparator === "number"
137
- ? comparator.toString()
138
- : comparator.trim()).toLowerCase();
139
- return cleanValue === cleanComparator && parent;
148
+ return cleanString(value) === cleanString(comparator) && parent;
140
149
  });
141
150
  var filter = reducedValues.filter(function (_value) { return _value === true; });
142
151
  return reducedValues.length === filter.length;
@@ -144,13 +153,7 @@ var is = function (values, comparator, parents) {
144
153
  json_logic_js_1.default.add_operation("is", is);
145
154
  var not = function (values, comparator, parents) {
146
155
  var reducedValues = reduceValues(values, parents, function (value, parent) {
147
- var cleanValue = (typeof value === "number"
148
- ? value.toString()
149
- : value.trim()).toLowerCase();
150
- var cleanComparator = (typeof comparator === "number"
151
- ? comparator.toString()
152
- : comparator.trim()).toLowerCase();
153
- return cleanValue !== cleanComparator && parent;
156
+ return cleanString(value) !== cleanString(comparator) && parent;
154
157
  });
155
158
  var filter = reducedValues.filter(function (_value) { return _value === true; });
156
159
  return reducedValues.length === filter.length;
@@ -159,21 +162,11 @@ json_logic_js_1.default.add_operation("not", not);
159
162
  var contains = function (values, comparator, parents) {
160
163
  var reducedValues = reduceValues(values, parents, function (value, parent) {
161
164
  if (Array.isArray(value)) {
162
- return value.some(function (val) {
163
- return (typeof val === "string" ? val.trim() : val.toString())
164
- .toLowerCase()
165
- .includes((typeof comparator === "string"
166
- ? comparator.trim()
167
- : comparator.toString()).toLowerCase()) && parent;
168
- });
169
- }
170
- else {
171
- var cleanValue = (typeof value === "number" ? value.toString() : value.trim()).toLowerCase();
172
- var cleanComparator = (typeof comparator === "number"
173
- ? comparator.toString()
174
- : comparator.trim()).toLowerCase();
175
- return cleanValue.includes(cleanComparator) && parent;
165
+ return (value.some(function (val) {
166
+ return cleanString(val).includes(cleanString(comparator));
167
+ }) && parent);
176
168
  }
169
+ return (cleanString(value).includes(cleanString(comparator)) && parent);
177
170
  });
178
171
  var filter = reducedValues.filter(function (value) { return value === true; });
179
172
  return reducedValues.length === filter.length;
@@ -182,21 +175,9 @@ json_logic_js_1.default.add_operation("contains", contains);
182
175
  var doesNotContain = function (values, comparator, parents) {
183
176
  var reducedValues = reduceValues(values, parents, function (value, parent) {
184
177
  if (Array.isArray(value)) {
185
- return value.every(function (val) {
186
- return !(typeof val === "string" ? val.trim() : val.toString())
187
- .toLowerCase()
188
- .includes((typeof comparator === "string"
189
- ? comparator.trim()
190
- : comparator.toString()).toLowerCase()) && parent;
191
- });
192
- }
193
- else {
194
- var cleanValue = (typeof value === "number" ? value.toString() : value.trim()).toLowerCase();
195
- var cleanComparator = (typeof comparator === "number"
196
- ? comparator.toString()
197
- : comparator.trim()).toLowerCase();
198
- return !cleanValue.includes(cleanComparator) && parent;
178
+ return (value.every(function (val) { return !cleanString(val).includes(cleanString(comparator)); }) && parent);
199
179
  }
180
+ return (!cleanString(value).includes(cleanString(comparator)) && parent);
200
181
  });
201
182
  var filter = reducedValues.filter(function (value) { return value === true; });
202
183
  return reducedValues.length === filter.length;
@@ -220,13 +201,7 @@ json_logic_js_1.default.add_operation("not-empty", function (values, comparator,
220
201
  });
221
202
  var like = function (values, comparator, parents) {
222
203
  var reducedValues = reduceValues(values, parents, function (value, parent) {
223
- var cleanValue = (typeof value === "number"
224
- ? value.toString()
225
- : value.trim()).toLowerCase();
226
- var cleanComparator = (typeof comparator === "number"
227
- ? comparator.toString()
228
- : comparator.trim()).toLowerCase();
229
- return (new RegExp("( |^)".concat(cleanComparator, "( |$)"), "i").test(cleanValue) && parent);
204
+ return (new RegExp("( |^)".concat(cleanString(comparator), "( |$)"), "i").test(cleanString(value)) && parent);
230
205
  });
231
206
  var filter = reducedValues.filter(function (_value) { return _value === true; });
232
207
  return reducedValues.length === filter.length;
@@ -234,14 +209,7 @@ var like = function (values, comparator, parents) {
234
209
  json_logic_js_1.default.add_operation("like", like);
235
210
  var notLike = function (values, comparator, parents) {
236
211
  var reducedValues = reduceValues(values, parents, function (value, parent) {
237
- var cleanValue = (typeof value === "number"
238
- ? value.toString()
239
- : value.trim()).toLowerCase();
240
- var cleanComparator = (typeof comparator === "number"
241
- ? comparator.toString()
242
- : comparator.trim()).toLowerCase();
243
- return (!new RegExp("( |^)".concat(cleanComparator, "( |$)"), "i").test(cleanValue) &&
244
- parent);
212
+ return (!new RegExp("( |^)".concat(cleanString(comparator), "( |$)"), "i").test(cleanString(value)) && parent);
245
213
  });
246
214
  var filter = reducedValues.filter(function (_value) { return _value === true; });
247
215
  return reducedValues.length === filter.length;
@@ -277,9 +245,6 @@ var aggregateAny = function (reducedValues) {
277
245
  var aggregateFirst = function (reducedValues) {
278
246
  return reducedValues[0] === true;
279
247
  };
280
- var cleanString = function (value) {
281
- return (typeof value === "number" ? value.toString() : value.trim()).toLowerCase();
282
- };
283
248
  var registerScopedBooleanOp = function (baseName, compareFn) {
284
249
  json_logic_js_1.default.add_operation("".concat(baseName, "-first"), function (values, _comparator, parents) {
285
250
  var reducedValue = reduceValues(values, parents, compareFn);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@legalplace/lplogic",
3
- "version": "2.3.2-staging.0",
3
+ "version": "2.3.2-staging.1",
4
4
  "description": "LegalPlace LpLogic",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -255,3 +255,116 @@ test('Testing "conditions-met" with parents containing missing refs (null entrie
255
255
  )
256
256
  ).toBe(true)
257
257
  })
258
+
259
+ /**
260
+ * Accent- and case-insensitive text matching (=, contains, like and negations).
261
+ * Matches the product table: la/lax, la/la ha, la/là, la/La, Léon/Leon.
262
+ */
263
+ test('Testing "=" case-insensitive for strings', async () => {
264
+ expect(LpLogic({ "=": [{ var: "v.1" }, "la"] }, { v: { 1: ["La"] } })).toBe(
265
+ true
266
+ )
267
+ expect(LpLogic({ "=": [{ var: "v.1" }, "la"] }, { v: { 1: ["LA"] } })).toBe(
268
+ true
269
+ )
270
+ })
271
+
272
+ test('Testing "=" accent-insensitive for strings', async () => {
273
+ expect(LpLogic({ "=": [{ var: "v.1" }, "la"] }, { v: { 1: ["là"] } })).toBe(
274
+ true
275
+ )
276
+ expect(
277
+ LpLogic({ "=": [{ var: "v.1" }, "Léon"] }, { v: { 1: ["Leon"] } })
278
+ ).toBe(true)
279
+ expect(
280
+ LpLogic({ "=": [{ var: "v.1" }, "Léon"] }, { v: { 1: ["LEON"] } })
281
+ ).toBe(true)
282
+ })
283
+
284
+ test('Testing "=" still false for different words', async () => {
285
+ expect(LpLogic({ "=": [{ var: "v.1" }, "la"] }, { v: { 1: ["lax"] } })).toBe(
286
+ false
287
+ )
288
+ expect(
289
+ LpLogic({ "=": [{ var: "v.1" }, "la"] }, { v: { 1: ["la ha"] } })
290
+ ).toBe(false)
291
+ })
292
+
293
+ test('Testing "=" still numeric for number-like values', async () => {
294
+ expect(LpLogic({ "=": [{ var: "v.1" }, "1.0"] }, { v: { 1: [1] } })).toBe(
295
+ true
296
+ )
297
+ expect(LpLogic({ "=": [{ var: "v.1" }, 1] }, { v: { 1: ["1"] } })).toBe(true)
298
+ })
299
+
300
+ test('Testing "<>" is inverse of accent/case-insensitive "="', async () => {
301
+ expect(LpLogic({ "<>": [{ var: "v.1" }, "la"] }, { v: { 1: ["là"] } })).toBe(
302
+ false
303
+ )
304
+ expect(LpLogic({ "<>": [{ var: "v.1" }, "la"] }, { v: { 1: ["lax"] } })).toBe(
305
+ true
306
+ )
307
+ })
308
+
309
+ test('Testing "contains" accent- and case-insensitive', async () => {
310
+ expect(
311
+ LpLogic({ contains: [{ var: "v.1" }, "la"] }, { v: { 1: ["lax"] } })
312
+ ).toBe(true)
313
+ expect(
314
+ LpLogic({ contains: [{ var: "v.1" }, "la"] }, { v: { 1: ["la ha"] } })
315
+ ).toBe(true)
316
+ expect(
317
+ LpLogic({ contains: [{ var: "v.1" }, "la"] }, { v: { 1: ["là"] } })
318
+ ).toBe(true)
319
+ expect(
320
+ LpLogic({ contains: [{ var: "v.1" }, "la"] }, { v: { 1: ["La"] } })
321
+ ).toBe(true)
322
+ })
323
+
324
+ test('Testing "does-not-contain" is inverse of accent-insensitive contains', async () => {
325
+ expect(
326
+ LpLogic(
327
+ { "does-not-contain": [{ var: "v.1" }, "la"] },
328
+ { v: { 1: ["là"] } }
329
+ )
330
+ ).toBe(false)
331
+ expect(
332
+ LpLogic(
333
+ { "does-not-contain": [{ var: "v.1" }, "la"] },
334
+ { v: { 1: ["Bye"] } }
335
+ )
336
+ ).toBe(true)
337
+ })
338
+
339
+ test('Testing "like" accent- and case-insensitive word match', async () => {
340
+ expect(
341
+ LpLogic({ like: [{ var: "v.1" }, "la"] }, { v: { 1: ["lax"] } })
342
+ ).toBe(false)
343
+ expect(
344
+ LpLogic({ like: [{ var: "v.1" }, "la"] }, { v: { 1: ["la ha"] } })
345
+ ).toBe(true)
346
+ expect(
347
+ LpLogic({ like: [{ var: "v.1" }, "la"] }, { v: { 1: ["là"] } })
348
+ ).toBe(true)
349
+ expect(
350
+ LpLogic({ like: [{ var: "v.1" }, "la"] }, { v: { 1: ["La"] } })
351
+ ).toBe(true)
352
+ })
353
+
354
+ test('Testing "not-like" is inverse of accent-insensitive like', async () => {
355
+ expect(
356
+ LpLogic({ "not-like": [{ var: "v.1" }, "la"] }, { v: { 1: ["là"] } })
357
+ ).toBe(false)
358
+ expect(
359
+ LpLogic({ "not-like": [{ var: "v.1" }, "la"] }, { v: { 1: ["lax"] } })
360
+ ).toBe(true)
361
+ })
362
+
363
+ test('Testing "is" accent-insensitive (legacy equal alias)', async () => {
364
+ expect(LpLogic({ is: [{ var: "v.1" }, "la"] }, { v: { 1: ["là"] } })).toBe(
365
+ true
366
+ )
367
+ expect(
368
+ LpLogic({ is: [{ var: "v.1" }, "Léon"] }, { v: { 1: ["Leon"] } })
369
+ ).toBe(true)
370
+ })