cafe-utility 6.1.0 → 6.2.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.
Files changed (3) hide show
  1. package/index.d.ts +3 -1
  2. package/index.js +22 -5
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -139,6 +139,7 @@ interface BlockExtractionOptions {
139
139
  exclusive?: boolean;
140
140
  }
141
141
  declare function extractBlock(string: string, options: BlockExtractionOptions): string | null;
142
+ declare function extractAllBlocks(string: string, options: BlockExtractionOptions): string[];
142
143
  declare function parseCsv(string: string, delimiter?: string, quote?: string): string[];
143
144
  declare function humanizeProgress(state: Progress): string;
144
145
  declare function waitFor(predicate: () => Promise<boolean>, waitLength: number, maxWaits: number): Promise<boolean>;
@@ -210,7 +211,7 @@ declare function increment(value: number, change: number, maximum: number): numb
210
211
  declare function decrement(value: number, change: number, minimum: number): number;
211
212
  declare function runOn<T>(object: T, callable: (object: T) => void): T;
212
213
  declare function ifPresent<T>(object: T, callable: (object: T) => void): void;
213
- declare function mergeArrays(target: CafeObject<unknown[]>, source: CafeObject<unknown[]>): void;
214
+ declare function mergeArrays(target: CafeObject<unknown | unknown[]>, source: CafeObject<unknown | unknown[]>): void;
214
215
  declare function empty<T>(array: T[]): T[];
215
216
  declare function removeEmptyArrays(object: CafeObject): CafeObject;
216
217
  declare function removeEmptyValues(object: CafeObject): CafeObject;
@@ -486,6 +487,7 @@ export declare const Strings: {
486
487
  joinHumanly: typeof joinHumanly;
487
488
  findWeightedPair: typeof findWeightedPair;
488
489
  extractBlock: typeof extractBlock;
490
+ extractAllBlocks: typeof extractAllBlocks;
489
491
  isLetter: typeof isLetter;
490
492
  isDigit: typeof isDigit;
491
493
  isLetterOrDigit: typeof isLetterOrDigit;
package/index.js CHANGED
@@ -885,11 +885,11 @@ function csvEscape(string) {
885
885
  function findWeightedPair(string, start = 0, opening = '{', closing = '}') {
886
886
  let weight = 1
887
887
  for (let i = start; i < string.length; i++) {
888
- if (string[i] === closing) {
888
+ if (string.slice(i, i + closing.length) === closing) {
889
889
  if (--weight === 0) {
890
890
  return i
891
891
  }
892
- } else if (string[i] === opening) {
892
+ } else if (string.slice(i, i + opening.length) === opening) {
893
893
  weight++
894
894
  }
895
895
  }
@@ -901,12 +901,28 @@ function extractBlock(string, options) {
901
901
  if (opensAt === -1) {
902
902
  return null
903
903
  }
904
- const closesAt = findWeightedPair(string, opensAt + 1, options.opening, options.closing)
904
+ const closesAt = findWeightedPair(string, opensAt + options.opening.length, options.opening, options.closing)
905
905
  return closesAt === -1
906
906
  ? null
907
907
  : options.exclusive
908
- ? string.slice(opensAt + 1, closesAt)
909
- : string.slice(opensAt, closesAt + 1)
908
+ ? string.slice(opensAt + options.opening.length, closesAt)
909
+ : string.slice(opensAt, closesAt + options.closing.length)
910
+ }
911
+
912
+ function extractAllBlocks(string, options) {
913
+ const blocks = []
914
+ let start = string.indexOf(options.opening)
915
+ while (true) {
916
+ if (start === -1) {
917
+ return blocks
918
+ }
919
+ const block = extractBlock(string, { ...options, start })
920
+ if (!block) {
921
+ return blocks
922
+ }
923
+ blocks.push(block)
924
+ start = string.indexOf(options.opening, start + block.length)
925
+ }
910
926
  }
911
927
 
912
928
  function parseCsv(string, delimiter = ',', quote = '"') {
@@ -2123,6 +2139,7 @@ exports.Strings = {
2123
2139
  joinHumanly,
2124
2140
  findWeightedPair,
2125
2141
  extractBlock,
2142
+ extractAllBlocks,
2126
2143
  isLetter,
2127
2144
  isDigit,
2128
2145
  isLetterOrDigit,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "6.1.0",
3
+ "version": "6.2.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {