@labdigital/commercetools-mock 2.30.1 → 2.31.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/index.cjs CHANGED
@@ -2438,7 +2438,7 @@ var CartUpdateHandler = class extends AbstractUpdateHandler {
2438
2438
  resource.itemShippingAddresses.push(newAddress);
2439
2439
  }
2440
2440
  }
2441
- addLineItem(context, resource, { productId, variantId, sku, quantity = 1 }) {
2441
+ addLineItem(context, resource, { productId, variantId, sku, custom, quantity = 1 }) {
2442
2442
  let product = null;
2443
2443
  if (productId && variantId) {
2444
2444
  product = this._storage.get(context.projectKey, "product", productId, {});
@@ -2522,7 +2522,8 @@ var CartUpdateHandler = class extends AbstractUpdateHandler {
2522
2522
  discountedPricePerQuantity: [],
2523
2523
  lineItemMode: "Standard",
2524
2524
  priceMode: "Platform",
2525
- state: []
2525
+ state: [],
2526
+ custom: createCustomFields(custom, context.projectKey, this._storage)
2526
2527
  });
2527
2528
  }
2528
2529
  resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
@@ -4544,19 +4545,346 @@ var PaymentRepository = class extends AbstractResourceRepository {
4544
4545
  }
4545
4546
  };
4546
4547
 
4547
- // src/lib/searchQueryTypeChecker.ts
4548
- var validateSearchQuery = (query) => {
4549
- if (isSearchAndExpression(query)) {
4550
- query.and.forEach((expr) => validateSearchQuery(expr));
4551
- } else if (isSearchOrExpression(query)) {
4552
- query.or.forEach((expr) => validateSearchQuery(expr));
4553
- } else if (isSearchNotExpression(query)) {
4554
- validateSearchQuery(query.not);
4555
- } else if (isSearchFilterExpression(query) || isSearchRangeExpression(query) || isSearchExactExpression(query) || isSearchExistsExpression(query) || isSearchFullTextExpression(query) || isSearchFullTextPrefixExpression(query) || isSearchPrefixExpression(query) || isSearchWildCardExpression(query) || isSearchAnyValue(query)) {
4556
- return;
4557
- } else {
4558
- throw new Error("Unsupported search query expression");
4548
+ // src/lib/projectionSearchFilter.ts
4549
+ var parseFilterExpression = (filter) => {
4550
+ const exprFunc = generateMatchFunc2(filter);
4551
+ const [source] = filter.split(":", 1);
4552
+ if (source.startsWith("variants.")) {
4553
+ return filterVariants(source, exprFunc);
4554
+ }
4555
+ return filterProduct(source, exprFunc);
4556
+ };
4557
+ var getLexer2 = (value) => new Lexer(value).token("MISSING", /missing(?![-_a-z0-9]+)/i).token("EXISTS", /exists(?![-_a-z0-9]+)/i).token("RANGE", /range(?![-_a-z0-9]+)/i).token("TO", /to(?![-_a-z0-9]+)/i).token("IDENTIFIER", /[-_.a-z]+/i).token("FLOAT", /\d+\.\d+/).token("INT", /\d+/).token("STRING", /"((?:\\.|[^"\\])*)"/).token("STRING", /'((?:\\.|[^'\\])*)'/).token("COMMA", ",").token("STAR", "*").token("(", "(").token(":", ":").token(")", ")").token('"', '"').token("WS", /\s+/, true);
4558
+ var parseFilter = (filter) => {
4559
+ const lexer = getLexer2(filter);
4560
+ const parser = new Parser(lexer).builder().nud("IDENTIFIER", 100, (t) => t.token.match).led(":", 100, ({ left, bp }) => {
4561
+ const parsed = parser.parse({ terminals: [bp - 1] });
4562
+ const expressions = !Array.isArray(parsed) ? [parsed] : parsed;
4563
+ const unique = new Set(expressions.map((expr) => expr.type));
4564
+ if (unique.size > 1) {
4565
+ throw new Error("Invalid expression");
4566
+ }
4567
+ if (expressions.some((expr) => expr.type == "Symbol")) {
4568
+ return {
4569
+ source: left,
4570
+ type: "FilterExpression",
4571
+ children: expressions.map((e) => {
4572
+ if (e.type != "Symbol") {
4573
+ throw new Error("Invalid expression");
4574
+ }
4575
+ return {
4576
+ type: "FilterExpression",
4577
+ match: (obj) => obj === e.value
4578
+ };
4579
+ })
4580
+ };
4581
+ }
4582
+ return {
4583
+ source: left,
4584
+ type: expressions[0].type,
4585
+ children: expressions
4586
+ };
4587
+ }).nud(
4588
+ "STRING",
4589
+ 20,
4590
+ (t) => ({
4591
+ type: "Symbol",
4592
+ kind: "string",
4593
+ // @ts-ignore
4594
+ value: t.token.groups[1]
4595
+ })
4596
+ ).nud(
4597
+ "INT",
4598
+ 5,
4599
+ (t) => ({
4600
+ type: "Symbol",
4601
+ kind: "int",
4602
+ value: parseInt(t.token.match, 10)
4603
+ })
4604
+ ).nud("STAR", 5, (_) => ({
4605
+ type: "Symbol",
4606
+ kind: "any",
4607
+ value: null
4608
+ })).nud(
4609
+ "EXISTS",
4610
+ 10,
4611
+ ({ bp }) => ({
4612
+ type: "FilterExpression",
4613
+ match: (obj) => obj !== void 0
4614
+ })
4615
+ ).nud(
4616
+ "MISSING",
4617
+ 10,
4618
+ ({ bp }) => ({
4619
+ type: "FilterExpression",
4620
+ match: (obj) => obj === void 0
4621
+ })
4622
+ ).led("COMMA", 200, ({ left, token, bp }) => {
4623
+ const expr = parser.parse({ terminals: [bp - 1] });
4624
+ if (Array.isArray(expr)) {
4625
+ return [left, ...expr];
4626
+ } else {
4627
+ return [left, expr];
4628
+ }
4629
+ }).nud("(", 100, (t) => {
4630
+ const expr = parser.parse({ terminals: [")"] });
4631
+ lexer.expect(")");
4632
+ return expr;
4633
+ }).bp(")", 0).led("TO", 20, ({ left, bp }) => {
4634
+ const expr = parser.parse({ terminals: [bp - 1] });
4635
+ return {
4636
+ start: left.value,
4637
+ stop: expr.value
4638
+ };
4639
+ }).nud("RANGE", 20, ({ bp }) => {
4640
+ let ranges = parser.parse();
4641
+ if (!Array.isArray(ranges)) {
4642
+ ranges = [ranges];
4643
+ }
4644
+ return ranges.map((range) => {
4645
+ let func;
4646
+ if (range.start !== null && range.stop !== null) {
4647
+ func = (obj) => obj >= range.start && obj <= range.stop;
4648
+ } else if (range.start === null && range.stop !== null) {
4649
+ func = (obj) => obj <= range.stop;
4650
+ } else if (range.start !== null && range.stop === null) {
4651
+ func = (obj) => obj >= range.start;
4652
+ } else {
4653
+ func = (obj) => true;
4654
+ }
4655
+ return {
4656
+ type: "RangeExpression",
4657
+ start: range.start,
4658
+ stop: range.stop,
4659
+ match: func
4660
+ };
4661
+ });
4662
+ }).build();
4663
+ return parser.parse();
4664
+ };
4665
+ var generateMatchFunc2 = (filter) => {
4666
+ const result = parseFilter(filter);
4667
+ if (!result) {
4668
+ throw new Error(`Syntax error while parsing '${filter}'.`);
4669
+ }
4670
+ if (result.type == "TermExpression") {
4671
+ throw new Error(`Syntax error while parsing '${filter}'.`);
4672
+ }
4673
+ return (obj) => {
4674
+ if (!result.children)
4675
+ return false;
4676
+ return result.children.some((c) => c.match(obj));
4677
+ };
4678
+ };
4679
+ var generateFacetFunc = (filter) => {
4680
+ if (!filter.includes(":")) {
4681
+ return {
4682
+ source: filter,
4683
+ type: "TermExpression"
4684
+ };
4685
+ }
4686
+ return parseFilter(filter);
4687
+ };
4688
+ var filterProduct = (source, exprFunc) => (p, markMatchingVariants) => {
4689
+ const value = nestedLookup(p, source);
4690
+ return exprFunc(value);
4691
+ };
4692
+ var filterVariants = (source, exprFunc) => (p, markMatchingVariants) => {
4693
+ const [, ...paths] = source.split(".");
4694
+ const path = paths.join(".");
4695
+ const variants = getVariants(p);
4696
+ for (const variant of variants) {
4697
+ const value = resolveVariantValue(variant, path);
4698
+ if (exprFunc(value)) {
4699
+ if (markMatchingVariants) {
4700
+ for (const v of variants) {
4701
+ v.isMatchingVariant = false;
4702
+ }
4703
+ variant.isMatchingVariant = true;
4704
+ }
4705
+ return true;
4706
+ }
4707
+ }
4708
+ return false;
4709
+ };
4710
+ var resolveVariantValue = (obj, path) => {
4711
+ if (path === void 0) {
4712
+ return obj;
4713
+ }
4714
+ if (path.startsWith("variants.")) {
4715
+ path = path.substring(path.indexOf(".") + 1);
4716
+ }
4717
+ if (path.startsWith("attributes.")) {
4718
+ const [, attrName, ...rest] = path.split(".");
4719
+ if (!obj.attributes) {
4720
+ return void 0;
4721
+ }
4722
+ for (const attr of obj.attributes) {
4723
+ if (attr.name === attrName) {
4724
+ return nestedLookup(attr.value, rest.join("."));
4725
+ }
4726
+ }
4727
+ }
4728
+ if (path === "price.centAmount") {
4729
+ return obj.prices && obj.prices.length > 0 ? obj.prices[0].value.centAmount : void 0;
4730
+ }
4731
+ return nestedLookup(obj, path);
4732
+ };
4733
+ var getVariants = (p) => [
4734
+ p.masterVariant,
4735
+ ...p.variants ?? []
4736
+ ];
4737
+
4738
+ // src/lib/productSearchFilter.ts
4739
+ var parseSearchQuery = (searchQuery) => {
4740
+ if (isSearchAndExpression(searchQuery)) {
4741
+ return (obj, markMatchingVariant) => searchQuery.and.every((expr) => {
4742
+ const filterFunc = parseSearchQuery(expr);
4743
+ return filterFunc(obj, markMatchingVariant);
4744
+ });
4745
+ }
4746
+ if (isSearchOrExpression(searchQuery)) {
4747
+ return (obj, markMatchingVariant) => searchQuery.or.some((expr) => {
4748
+ const filterFunc = parseSearchQuery(expr);
4749
+ return filterFunc(obj, markMatchingVariant);
4750
+ });
4751
+ }
4752
+ if (isSearchNotExpression(searchQuery)) {
4753
+ return (obj, markMatchingVariant) => !parseSearchQuery(searchQuery.not)(obj, markMatchingVariant);
4754
+ }
4755
+ if (isSearchFilterExpression(searchQuery)) {
4756
+ return (obj, markMatchingVariant) => searchQuery.filter.every((expr) => {
4757
+ const filterFunc = parseSearchQuery(expr);
4758
+ return filterFunc(obj, markMatchingVariant);
4759
+ });
4760
+ }
4761
+ if (isSearchRangeExpression(searchQuery)) {
4762
+ const generateRangeMatchFunc = (value) => {
4763
+ const rangeFilters = [];
4764
+ if (searchQuery.range.gte) {
4765
+ rangeFilters.push(value >= searchQuery.range.gte);
4766
+ }
4767
+ if (searchQuery.range.gt) {
4768
+ rangeFilters.push(value > searchQuery.range.gt);
4769
+ }
4770
+ if (searchQuery.range.lte) {
4771
+ rangeFilters.push(value <= searchQuery.range.lte);
4772
+ }
4773
+ if (searchQuery.range.lt) {
4774
+ rangeFilters.push(value < searchQuery.range.lt);
4775
+ }
4776
+ return rangeFilters.every((filter) => filter);
4777
+ };
4778
+ return generateFieldMatchFunc(generateRangeMatchFunc, searchQuery.range);
4779
+ }
4780
+ if (isSearchExactExpression(searchQuery)) {
4781
+ return generateFieldMatchFunc(
4782
+ (value) => value === searchQuery.exact.value,
4783
+ searchQuery.exact
4784
+ );
4785
+ }
4786
+ if (isSearchExistsExpression(searchQuery)) {
4787
+ return generateFieldMatchFunc((value) => !!value, searchQuery.exists);
4788
+ }
4789
+ if (isSearchFullTextExpression(searchQuery)) {
4790
+ return generateFieldMatchFunc(
4791
+ (value) => value.includes(searchQuery.fullText.value),
4792
+ searchQuery.fullText
4793
+ );
4794
+ }
4795
+ if (isSearchFullTextPrefixExpression(searchQuery)) {
4796
+ return generateFieldMatchFunc(
4797
+ (value) => value.startsWith(searchQuery.fullTextPrefix.value),
4798
+ searchQuery.fullTextPrefix
4799
+ );
4800
+ }
4801
+ if (isSearchPrefixExpression(searchQuery)) {
4802
+ return generateFieldMatchFunc(
4803
+ (value) => value.startsWith(searchQuery.prefix.value),
4804
+ searchQuery.prefix
4805
+ );
4806
+ }
4807
+ if (isSearchWildCardExpression(searchQuery)) {
4808
+ const generateWildcardMatchFunc = (value) => {
4809
+ const wildCardValues = searchQuery.wildcard.value.split("*").filter((v) => !!v);
4810
+ if (searchQuery.wildcard.caseInsensitive) {
4811
+ return wildCardValues.every(
4812
+ (wildCardValue) => value.toLowerCase().includes(wildCardValue.toLowerCase())
4813
+ );
4814
+ }
4815
+ return wildCardValues.every(
4816
+ (wildCardValue) => value.includes(wildCardValue)
4817
+ );
4818
+ };
4819
+ return generateFieldMatchFunc(
4820
+ generateWildcardMatchFunc,
4821
+ searchQuery.wildcard
4822
+ );
4823
+ }
4824
+ throw new Error("Unsupported search query expression");
4825
+ };
4826
+ var generateFieldMatchFunc = (matchFunc, searchQuery) => {
4827
+ const generateMatchFunc3 = (obj, markMatchingVariants) => {
4828
+ if (searchQuery.field.startsWith("variants.")) {
4829
+ const variantField = searchQuery.field.substring(
4830
+ searchQuery.field.indexOf(".") + 1
4831
+ );
4832
+ const variants = getVariants(obj);
4833
+ for (const variant of variants) {
4834
+ const value = resolveFieldValue(variant, {
4835
+ ...searchQuery,
4836
+ field: variantField
4837
+ });
4838
+ if (matchFunc(value)) {
4839
+ if (markMatchingVariants) {
4840
+ for (const v of variants) {
4841
+ v.isMatchingVariant = false;
4842
+ }
4843
+ variant.isMatchingVariant = true;
4844
+ }
4845
+ return true;
4846
+ }
4847
+ }
4848
+ return false;
4849
+ }
4850
+ return matchFunc(resolveFieldValue(obj, searchQuery));
4851
+ };
4852
+ return generateMatchFunc3;
4853
+ };
4854
+ var resolveFieldValue = (obj, searchQuery) => {
4855
+ if (searchQuery.field === void 0) {
4856
+ throw new Error("Missing field path in query expression");
4559
4857
  }
4858
+ let fieldPath = searchQuery.field;
4859
+ const language = "language" in searchQuery ? searchQuery.language : void 0;
4860
+ if (fieldPath.startsWith("variants.")) {
4861
+ fieldPath = fieldPath.substring(fieldPath.indexOf(".") + 1);
4862
+ }
4863
+ if (fieldPath.startsWith("attributes.")) {
4864
+ const [, attrName, ...rest] = fieldPath.split(".");
4865
+ if (!obj.attributes) {
4866
+ return void 0;
4867
+ }
4868
+ for (const attr of obj.attributes) {
4869
+ if (attr.name === attrName) {
4870
+ return nestedLookupByLanguage(attr.value, rest.join("."), language);
4871
+ }
4872
+ }
4873
+ }
4874
+ if (fieldPath === "prices.currentCentAmount") {
4875
+ return obj.prices && obj.prices.length > 0 ? obj.prices[0].value.centAmount : void 0;
4876
+ }
4877
+ return nestedLookupByLanguage(obj, fieldPath, language);
4878
+ };
4879
+ var nestedLookupByLanguage = (obj, path, language) => {
4880
+ const value = nestedLookup(obj, path);
4881
+ if (language && value && typeof value === "object") {
4882
+ const matchingLanguageKey = Object.keys(value).find(
4883
+ (key) => key.toLowerCase().startsWith(language.toLowerCase())
4884
+ );
4885
+ return matchingLanguageKey ? value[matchingLanguageKey] : void 0;
4886
+ }
4887
+ return value;
4560
4888
  };
4561
4889
  var isSearchAndExpression = (expr) => expr.and !== void 0;
4562
4890
  var isSearchOrExpression = (expr) => expr.or !== void 0;
@@ -4569,6 +4897,32 @@ var isSearchFullTextExpression = (expr) => expr.fullText !== void 0;
4569
4897
  var isSearchFullTextPrefixExpression = (expr) => expr.fullTextPrefix !== void 0;
4570
4898
  var isSearchPrefixExpression = (expr) => expr.prefix !== void 0;
4571
4899
  var isSearchWildCardExpression = (expr) => expr.wildcard !== void 0;
4900
+
4901
+ // src/lib/searchQueryTypeChecker.ts
4902
+ var validateSearchQuery = (query) => {
4903
+ if (isSearchAndExpression2(query)) {
4904
+ query.and.forEach((expr) => validateSearchQuery(expr));
4905
+ } else if (isSearchOrExpression2(query)) {
4906
+ query.or.forEach((expr) => validateSearchQuery(expr));
4907
+ } else if (isSearchNotExpression2(query)) {
4908
+ validateSearchQuery(query.not);
4909
+ } else if (isSearchFilterExpression2(query) || isSearchRangeExpression2(query) || isSearchExactExpression2(query) || isSearchExistsExpression2(query) || isSearchFullTextExpression2(query) || isSearchFullTextPrefixExpression2(query) || isSearchPrefixExpression2(query) || isSearchWildCardExpression2(query) || isSearchAnyValue(query)) {
4910
+ return;
4911
+ } else {
4912
+ throw new Error("Unsupported search query expression");
4913
+ }
4914
+ };
4915
+ var isSearchAndExpression2 = (expr) => expr.and !== void 0;
4916
+ var isSearchOrExpression2 = (expr) => expr.or !== void 0;
4917
+ var isSearchNotExpression2 = (expr) => expr.not !== void 0;
4918
+ var isSearchFilterExpression2 = (expr) => expr.filter !== void 0;
4919
+ var isSearchRangeExpression2 = (expr) => expr.range !== void 0;
4920
+ var isSearchExactExpression2 = (expr) => expr.exact !== void 0;
4921
+ var isSearchExistsExpression2 = (expr) => expr.exists !== void 0;
4922
+ var isSearchFullTextExpression2 = (expr) => expr.fullText !== void 0;
4923
+ var isSearchFullTextPrefixExpression2 = (expr) => expr.fullTextPrefix !== void 0;
4924
+ var isSearchPrefixExpression2 = (expr) => expr.prefix !== void 0;
4925
+ var isSearchWildCardExpression2 = (expr) => expr.wildcard !== void 0;
4572
4926
  var isSearchAnyValue = (expr) => expr.value !== void 0;
4573
4927
 
4574
4928
  // src/priceSelector.ts
@@ -4629,7 +4983,7 @@ var ProductSearch = class {
4629
4983
  this._storage = storage;
4630
4984
  }
4631
4985
  search(projectKey, params) {
4632
- const resources = this._storage.all(projectKey, "product").map(
4986
+ let resources = this._storage.all(projectKey, "product").map(
4633
4987
  (r) => this.transform(r, params.productProjectionParameters?.staged ?? false)
4634
4988
  ).filter((p) => {
4635
4989
  if (!params.productProjectionParameters?.staged) {
@@ -4637,9 +4991,14 @@ var ProductSearch = class {
4637
4991
  }
4638
4992
  return true;
4639
4993
  });
4994
+ const markMatchingVariant = params.markMatchingVariants ?? false;
4640
4995
  if (params.query) {
4641
4996
  try {
4642
4997
  validateSearchQuery(params.query);
4998
+ const matchFunc = parseSearchQuery(params.query);
4999
+ resources = resources.filter(
5000
+ (resource) => matchFunc(resource, markMatchingVariant)
5001
+ );
4643
5002
  } catch (err) {
4644
5003
  console.error(err);
4645
5004
  throw new CommercetoolsError(
@@ -5616,196 +5975,6 @@ var ProductDiscountUpdateHandler = class extends AbstractUpdateHandler {
5616
5975
  }
5617
5976
  };
5618
5977
 
5619
- // src/lib/projectionSearchFilter.ts
5620
- var parseFilterExpression = (filter) => {
5621
- const exprFunc = generateMatchFunc2(filter);
5622
- const [source] = filter.split(":", 1);
5623
- if (source.startsWith("variants.")) {
5624
- return filterVariants(source, exprFunc);
5625
- }
5626
- return filterProduct(source, exprFunc);
5627
- };
5628
- var getLexer2 = (value) => new Lexer(value).token("MISSING", /missing(?![-_a-z0-9]+)/i).token("EXISTS", /exists(?![-_a-z0-9]+)/i).token("RANGE", /range(?![-_a-z0-9]+)/i).token("TO", /to(?![-_a-z0-9]+)/i).token("IDENTIFIER", /[-_.a-z]+/i).token("FLOAT", /\d+\.\d+/).token("INT", /\d+/).token("STRING", /"((?:\\.|[^"\\])*)"/).token("STRING", /'((?:\\.|[^'\\])*)'/).token("COMMA", ",").token("STAR", "*").token("(", "(").token(":", ":").token(")", ")").token('"', '"').token("WS", /\s+/, true);
5629
- var parseFilter = (filter) => {
5630
- const lexer = getLexer2(filter);
5631
- const parser = new Parser(lexer).builder().nud("IDENTIFIER", 100, (t) => t.token.match).led(":", 100, ({ left, bp }) => {
5632
- const parsed = parser.parse({ terminals: [bp - 1] });
5633
- const expressions = !Array.isArray(parsed) ? [parsed] : parsed;
5634
- const unique = new Set(expressions.map((expr) => expr.type));
5635
- if (unique.size > 1) {
5636
- throw new Error("Invalid expression");
5637
- }
5638
- if (expressions.some((expr) => expr.type == "Symbol")) {
5639
- return {
5640
- source: left,
5641
- type: "FilterExpression",
5642
- children: expressions.map((e) => {
5643
- if (e.type != "Symbol") {
5644
- throw new Error("Invalid expression");
5645
- }
5646
- return {
5647
- type: "FilterExpression",
5648
- match: (obj) => obj === e.value
5649
- };
5650
- })
5651
- };
5652
- }
5653
- return {
5654
- source: left,
5655
- type: expressions[0].type,
5656
- children: expressions
5657
- };
5658
- }).nud(
5659
- "STRING",
5660
- 20,
5661
- (t) => ({
5662
- type: "Symbol",
5663
- kind: "string",
5664
- // @ts-ignore
5665
- value: t.token.groups[1]
5666
- })
5667
- ).nud(
5668
- "INT",
5669
- 5,
5670
- (t) => ({
5671
- type: "Symbol",
5672
- kind: "int",
5673
- value: parseInt(t.token.match, 10)
5674
- })
5675
- ).nud("STAR", 5, (_) => ({
5676
- type: "Symbol",
5677
- kind: "any",
5678
- value: null
5679
- })).nud(
5680
- "EXISTS",
5681
- 10,
5682
- ({ bp }) => ({
5683
- type: "FilterExpression",
5684
- match: (obj) => obj !== void 0
5685
- })
5686
- ).nud(
5687
- "MISSING",
5688
- 10,
5689
- ({ bp }) => ({
5690
- type: "FilterExpression",
5691
- match: (obj) => obj === void 0
5692
- })
5693
- ).led("COMMA", 200, ({ left, token, bp }) => {
5694
- const expr = parser.parse({ terminals: [bp - 1] });
5695
- if (Array.isArray(expr)) {
5696
- return [left, ...expr];
5697
- } else {
5698
- return [left, expr];
5699
- }
5700
- }).nud("(", 100, (t) => {
5701
- const expr = parser.parse({ terminals: [")"] });
5702
- lexer.expect(")");
5703
- return expr;
5704
- }).bp(")", 0).led("TO", 20, ({ left, bp }) => {
5705
- const expr = parser.parse({ terminals: [bp - 1] });
5706
- return {
5707
- start: left.value,
5708
- stop: expr.value
5709
- };
5710
- }).nud("RANGE", 20, ({ bp }) => {
5711
- let ranges = parser.parse();
5712
- if (!Array.isArray(ranges)) {
5713
- ranges = [ranges];
5714
- }
5715
- return ranges.map((range) => {
5716
- let func;
5717
- if (range.start !== null && range.stop !== null) {
5718
- func = (obj) => obj >= range.start && obj <= range.stop;
5719
- } else if (range.start === null && range.stop !== null) {
5720
- func = (obj) => obj <= range.stop;
5721
- } else if (range.start !== null && range.stop === null) {
5722
- func = (obj) => obj >= range.start;
5723
- } else {
5724
- func = (obj) => true;
5725
- }
5726
- return {
5727
- type: "RangeExpression",
5728
- start: range.start,
5729
- stop: range.stop,
5730
- match: func
5731
- };
5732
- });
5733
- }).build();
5734
- return parser.parse();
5735
- };
5736
- var generateMatchFunc2 = (filter) => {
5737
- const result = parseFilter(filter);
5738
- if (!result) {
5739
- throw new Error(`Syntax error while parsing '${filter}'.`);
5740
- }
5741
- if (result.type == "TermExpression") {
5742
- throw new Error(`Syntax error while parsing '${filter}'.`);
5743
- }
5744
- return (obj) => {
5745
- if (!result.children)
5746
- return false;
5747
- return result.children.some((c) => c.match(obj));
5748
- };
5749
- };
5750
- var generateFacetFunc = (filter) => {
5751
- if (!filter.includes(":")) {
5752
- return {
5753
- source: filter,
5754
- type: "TermExpression"
5755
- };
5756
- }
5757
- return parseFilter(filter);
5758
- };
5759
- var filterProduct = (source, exprFunc) => (p, markMatchingVariants) => {
5760
- const value = nestedLookup(p, source);
5761
- return exprFunc(value);
5762
- };
5763
- var filterVariants = (source, exprFunc) => (p, markMatchingVariants) => {
5764
- const [, ...paths] = source.split(".");
5765
- const path = paths.join(".");
5766
- const variants = getVariants(p);
5767
- for (const variant of variants) {
5768
- const value = resolveVariantValue(variant, path);
5769
- if (exprFunc(value)) {
5770
- if (markMatchingVariants) {
5771
- for (const v of variants) {
5772
- v.isMatchingVariant = false;
5773
- }
5774
- variant.isMatchingVariant = true;
5775
- }
5776
- return true;
5777
- }
5778
- }
5779
- return false;
5780
- };
5781
- var resolveVariantValue = (obj, path) => {
5782
- if (path === void 0) {
5783
- return obj;
5784
- }
5785
- if (path.startsWith("variants.")) {
5786
- path = path.substring(path.indexOf(".") + 1);
5787
- }
5788
- if (path.startsWith("attributes.")) {
5789
- const [, attrName, ...rest] = path.split(".");
5790
- if (!obj.attributes) {
5791
- return void 0;
5792
- }
5793
- for (const attr of obj.attributes) {
5794
- if (attr.name === attrName) {
5795
- return nestedLookup(attr.value, rest.join("."));
5796
- }
5797
- }
5798
- }
5799
- if (path === "price.centAmount") {
5800
- return obj.prices && obj.prices.length > 0 ? obj.prices[0].value.centAmount : void 0;
5801
- }
5802
- return nestedLookup(obj, path);
5803
- };
5804
- var getVariants = (p) => [
5805
- p.masterVariant,
5806
- ...p.variants ?? []
5807
- ];
5808
-
5809
5978
  // src/product-projection-search.ts
5810
5979
  var ProductProjectionSearch = class {
5811
5980
  _storage;