cafe-utility 6.9.0 → 7.0.0

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.
Files changed (3) hide show
  1. package/index.d.ts +10 -2
  2. package/index.js +43 -7
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -130,7 +130,13 @@ declare function isDigit(character: string): boolean;
130
130
  declare function isLetterOrDigit(character: string): boolean;
131
131
  declare function isValidObjectPathCharacter(character: string): boolean;
132
132
  declare function insertString(string: string, index: number, length: number, before: string, after: string): string;
133
- declare function linesMatchOrdered(lines: string[], expectedLines: (string | RegExp)[]): boolean;
133
+ interface RegexMatch {
134
+ index: number;
135
+ match: string;
136
+ }
137
+ declare function indexOfRegex(string: string, regex: RegExp, start?: number): RegexMatch | null;
138
+ declare function lineMatches(haystack: string, needles: (string | RegExp)[], orderMatters?: boolean): boolean;
139
+ declare function linesMatchInOrder(lines: string[], expectations: (string[] | RegExp[])[], orderMatters?: boolean): boolean;
134
140
  declare function csvEscape(string: string): string;
135
141
  declare function indexOfEarliest(string: string, searchStrings: string[], start?: number): number;
136
142
  declare function findWeightedPair(string: string, start?: number, opening?: string, closing?: string): number;
@@ -501,7 +507,9 @@ export declare const Strings: {
501
507
  isLetterOrDigit: typeof isLetterOrDigit;
502
508
  isValidObjectPathCharacter: typeof isValidObjectPathCharacter;
503
509
  insert: typeof insertString;
504
- linesMatchOrdered: typeof linesMatchOrdered;
510
+ indexOfRegex: typeof indexOfRegex;
511
+ lineMatches: typeof lineMatches;
512
+ linesMatchInOrder: typeof linesMatchInOrder;
505
513
  represent: typeof represent;
506
514
  };
507
515
  export declare const Assertions: {
package/index.js CHANGED
@@ -882,15 +882,49 @@ function insertString(string, index, length, before, after) {
882
882
  return string.slice(0, index) + before + string.slice(index, index + length) + after + string.slice(index + length)
883
883
  }
884
884
 
885
- function linesMatchOrdered(lines, expectedLines) {
885
+ function indexOfRegex(string, regex, start = 0) {
886
+ const match = regex.exec(string.slice(start))
887
+ return match ? { index: match.index, match: match[0] } : null
888
+ }
889
+
890
+ function lineMatches(haystack, needles, orderMatters = true) {
891
+ if (!orderMatters) {
892
+ return needles.every(needle => {
893
+ if (needle instanceof RegExp) {
894
+ return needle.test(haystack)
895
+ }
896
+ const position = haystack.indexOf(needle, 0)
897
+ if (position === -1) {
898
+ return false
899
+ }
900
+ return true
901
+ })
902
+ }
903
+ let index = 0
904
+ for (const needle of needles) {
905
+ if (needle instanceof RegExp) {
906
+ const match = indexOfRegex(haystack, needle, index)
907
+ if (!match) {
908
+ return false
909
+ }
910
+ index = match.index + match.match.length
911
+ } else {
912
+ const position = haystack.indexOf(needle, index)
913
+ if (position === -1) {
914
+ return false
915
+ }
916
+ index = position + needle.length
917
+ }
918
+ }
919
+ return true
920
+ }
921
+
922
+ function linesMatchInOrder(lines, expectations, orderMatters = true) {
886
923
  let lineIndex = 0
887
- for (let i = 0; i < expectedLines.length; i++) {
888
- const expectedLine = expectedLines[i]
924
+ for (const expectation of expectations) {
889
925
  let found = false
890
926
  while (!found && lineIndex < lines.length) {
891
- if (expectedLine instanceof RegExp) {
892
- found = expectedLine.test(lines[lineIndex])
893
- } else if (lines[lineIndex].includes(expectedLine)) {
927
+ if (lineMatches(lines[lineIndex], expectation, orderMatters)) {
894
928
  found = true
895
929
  }
896
930
  lineIndex++
@@ -2243,7 +2277,9 @@ exports.Strings = {
2243
2277
  isLetterOrDigit,
2244
2278
  isValidObjectPathCharacter,
2245
2279
  insert: insertString,
2246
- linesMatchOrdered,
2280
+ indexOfRegex,
2281
+ lineMatches,
2282
+ linesMatchInOrder,
2247
2283
  represent
2248
2284
  }
2249
2285
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "6.9.0",
3
+ "version": "7.0.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {