cafe-utility 4.10.0 → 4.11.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 -0
  2. package/index.js +30 -1
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -145,6 +145,14 @@ declare function shrinkTrim(string: string): string;
145
145
  declare function capitalize(string: string): string;
146
146
  declare function decapitalize(string: string): string;
147
147
  declare function csvEscape(string: string): string;
148
+ declare function findWeightedPair(string: string, start?: number, opening?: string, closing?: string): number;
149
+ interface BlockExtractionOptions {
150
+ start?: number;
151
+ opening: string;
152
+ closing: string;
153
+ exclusive?: boolean;
154
+ }
155
+ declare function extractBlock(string: string, options: BlockExtractionOptions): string | null;
148
156
  declare function parseCsv(string: string, delimiter?: string, quote?: string): string[];
149
157
  declare function humanizeProgress(state: Progress): string;
150
158
  declare function waitFor(predicate: () => Promise<boolean>, waitLength: number, maxWaits: number): Promise<boolean>;
@@ -487,6 +495,8 @@ export declare const Strings: {
487
495
  slugToTitle: typeof slugToTitle;
488
496
  slugToCamel: typeof slugToCamel;
489
497
  joinHumanly: typeof joinHumanly;
498
+ findWeightedPair: typeof findWeightedPair;
499
+ extractBlock: typeof extractBlock;
490
500
  };
491
501
  export declare const Assertions: {
492
502
  asEqual: typeof asEqual;
package/index.js CHANGED
@@ -931,6 +931,33 @@ function csvEscape(string) {
931
931
  return string.match(/"|,/) ? `"${string.replace(/"/g, '""')}"` : string
932
932
  }
933
933
 
934
+ function findWeightedPair(string, start = 0, opening = '{', closing = '}') {
935
+ let weight = 1
936
+ for (let i = start; i < string.length; i++) {
937
+ if (string[i] === opening) {
938
+ weight++
939
+ } else if (string[i] === closing) {
940
+ if (--weight === 0) {
941
+ return i
942
+ }
943
+ }
944
+ }
945
+ return -1
946
+ }
947
+
948
+ function extractBlock(string, options) {
949
+ const opensAt = string.indexOf(options.opening, options.start || 0)
950
+ if (opensAt === -1) {
951
+ return null
952
+ }
953
+ const closesAt = findWeightedPair(string, opensAt + 1, options.opening, options.closing)
954
+ return closesAt === -1
955
+ ? null
956
+ : options.exclusive
957
+ ? string.slice(opensAt + 1, closesAt)
958
+ : string.slice(opensAt, closesAt + 1)
959
+ }
960
+
934
961
  function parseCsv(string, delimiter = ',', quote = '"') {
935
962
  const items = []
936
963
  let buffer = ''
@@ -1930,7 +1957,9 @@ exports.Strings = {
1930
1957
  camelToTitle,
1931
1958
  slugToTitle,
1932
1959
  slugToCamel,
1933
- joinHumanly
1960
+ joinHumanly,
1961
+ findWeightedPair,
1962
+ extractBlock
1934
1963
  }
1935
1964
 
1936
1965
  exports.Assertions = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "4.10.0",
3
+ "version": "4.11.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {