cafe-utility 7.0.0 → 7.2.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 +5 -1
  2. package/index.js +101 -29
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -136,9 +136,10 @@ interface RegexMatch {
136
136
  }
137
137
  declare function indexOfRegex(string: string, regex: RegExp, start?: number): RegexMatch | null;
138
138
  declare function lineMatches(haystack: string, needles: (string | RegExp)[], orderMatters?: boolean): boolean;
139
- declare function linesMatchInOrder(lines: string[], expectations: (string[] | RegExp[])[], orderMatters?: boolean): boolean;
139
+ declare function linesMatchInOrder(lines: string[], expectations: (string | RegExp)[][], orderMatters?: boolean): boolean;
140
140
  declare function csvEscape(string: string): string;
141
141
  declare function indexOfEarliest(string: string, searchStrings: string[], start?: number): number;
142
+ declare function lastIndexOfBefore(string: string, searchString: string, start?: number): number;
142
143
  declare function findWeightedPair(string: string, start?: number, opening?: string, closing?: string): number;
143
144
  interface BlockExtractionOptions {
144
145
  start?: number;
@@ -153,6 +154,7 @@ declare function parseHtmlAttributes(string: string): Record<string, string>;
153
154
  declare function readNextWord(string: string, index: number, allowedCharacters?: string[]): string;
154
155
  declare function resolveVariableWithDefaultSyntax(string: string, key: string, value: string, prefix?: string, separator?: string): string;
155
156
  declare function resolveRemainingVariablesWithDefaults(string: string, prefix?: string, separator?: string): string;
157
+ declare function resolveMarkdownLinks(string: string, transformer: (label: string, link: string) => string): string;
156
158
  declare function parseCsv(string: string, delimiter?: string, quote?: string): string[];
157
159
  declare function humanizeProgress(state: Progress): string;
158
160
  declare function waitFor(predicate: () => Promise<boolean>, waitLength: number, maxWaits: number): Promise<boolean>;
@@ -498,6 +500,7 @@ export declare const Strings: {
498
500
  extractBlock: typeof extractBlock;
499
501
  extractAllBlocks: typeof extractAllBlocks;
500
502
  indexOfEarliest: typeof indexOfEarliest;
503
+ lastIndexOfBefore: typeof lastIndexOfBefore;
501
504
  parseHtmlAttributes: typeof parseHtmlAttributes;
502
505
  readNextWord: typeof readNextWord;
503
506
  resolveVariableWithDefaultSyntax: typeof resolveVariableWithDefaultSyntax;
@@ -511,6 +514,7 @@ export declare const Strings: {
511
514
  lineMatches: typeof lineMatches;
512
515
  linesMatchInOrder: typeof linesMatchInOrder;
513
516
  represent: typeof represent;
517
+ resolveMarkdownLinks: typeof resolveMarkdownLinks;
514
518
  };
515
519
  export declare const Assertions: {
516
520
  asEqual: typeof asEqual;
package/index.js CHANGED
@@ -951,6 +951,10 @@ function indexOfEarliest(string, searchStrings, start = 0) {
951
951
  return earliest
952
952
  }
953
953
 
954
+ function lastIndexOfBefore(string, searchString, start = 0) {
955
+ return string.slice(0, start).lastIndexOf(searchString)
956
+ }
957
+
954
958
  function findWeightedPair(string, start = 0, opening = '{', closing = '}') {
955
959
  let weight = 1
956
960
  for (let i = start; i < string.length; i++) {
@@ -989,7 +993,7 @@ function extractAllBlocks(string, options) {
989
993
  if (start === -1) {
990
994
  return blocks
991
995
  }
992
- const block = extractBlock(string, { ...options, start })
996
+ const block = extractBlock(string, Object.assign(Object.assign({}, options), { start }))
993
997
  if (!block) {
994
998
  return blocks
995
999
  }
@@ -1005,7 +1009,7 @@ function parseHtmlAttributes(string) {
1005
1009
  const matches = string.match(/([a-z\-]+)="([^"]+)"/g)
1006
1010
  if (matches) {
1007
1011
  for (const match of matches) {
1008
- const [name, value] = match.split('=')
1012
+ const [name, value] = splitOnce(match, '=')
1009
1013
  attributes[name] = value.slice(1, value.length - 1)
1010
1014
  }
1011
1015
  }
@@ -1021,12 +1025,19 @@ function readNextWord(string, index, allowedCharacters = []) {
1021
1025
  }
1022
1026
 
1023
1027
  function resolveVariableWithDefaultSyntax(string, key, value, prefix = '$', separator = ':') {
1028
+ if (value === '') {
1029
+ return string
1030
+ }
1024
1031
  let index = string.indexOf(`${prefix}${key}`)
1025
1032
  while (index !== -1) {
1026
1033
  const nextCharacter = string[index + key.length + 1]
1027
1034
  if (nextCharacter === separator) {
1028
- const nextWord = readNextWord(string, index + key.length + 2)
1029
- string = string.replace(`${prefix}${key}${separator}${nextWord}`, value)
1035
+ if (string[index + key.length + 2] === separator) {
1036
+ string = string.replace(`${prefix}${key}${separator}${separator}`, value)
1037
+ } else {
1038
+ const nextWord = readNextWord(string, index + key.length + 2)
1039
+ string = string.replace(`${prefix}${key}${separator}${nextWord}`, value)
1040
+ }
1030
1041
  } else {
1031
1042
  string = string.replace(`${prefix}${key}`, value)
1032
1043
  }
@@ -1041,14 +1052,33 @@ function resolveRemainingVariablesWithDefaults(string, prefix = '$', separator =
1041
1052
  const variableName = readNextWord(string, index + 1)
1042
1053
  const nextCharacter = string[index + variableName.length + 1]
1043
1054
  if (nextCharacter === separator) {
1044
- const nextWord = readNextWord(string, index + variableName.length + 2)
1045
- string = string.replace(`${prefix}${variableName}${separator}${nextWord}`, nextWord)
1055
+ if (string[index + variableName.length + 2] === separator) {
1056
+ string = string.replace(`${prefix}${variableName}${separator}${separator}`, '')
1057
+ } else {
1058
+ const nextWord = readNextWord(string, index + variableName.length + 2)
1059
+ string = string.replace(`${prefix}${variableName}${separator}${nextWord}`, nextWord)
1060
+ }
1046
1061
  }
1047
1062
  index = string.indexOf(prefix, index + 1)
1048
1063
  }
1049
1064
  return string
1050
1065
  }
1051
1066
 
1067
+ function resolveMarkdownLinks(string, transformer) {
1068
+ let index = string.indexOf('](')
1069
+ while (index !== -1) {
1070
+ const start = lastIndexOfBefore(string, '[', index)
1071
+ const end = string.indexOf(')', index)
1072
+ if (start !== -1 && end !== -1) {
1073
+ const [label, link] = string.slice(start + 1, end).split('](')
1074
+ const result = transformer(label, link)
1075
+ string = string.slice(0, start) + result + string.slice(end + 1)
1076
+ }
1077
+ index = string.indexOf('](', index + 1)
1078
+ }
1079
+ return string
1080
+ }
1081
+
1052
1082
  function parseCsv(string, delimiter = ',', quote = '"') {
1053
1083
  const items = []
1054
1084
  let buffer = ''
@@ -1366,10 +1396,13 @@ const longNumberUnits = [
1366
1396
  ]
1367
1397
  const shortNumberUnits = ['K', 'M', 'B', 't', 'q', 'Q', 's', 'S', 'o', 'n', 'd']
1368
1398
  function formatNumber(number, options) {
1369
- const longFormat = options?.longForm ?? false
1370
- const unitString = options?.unit ? ` ${options.unit}` : ''
1399
+ var _a, _b
1400
+ const longFormat =
1401
+ (_a = options === null || options === void 0 ? void 0 : options.longForm) !== null && _a !== void 0 ? _a : false
1402
+ const unitString = (options === null || options === void 0 ? void 0 : options.unit) ? ` ${options.unit}` : ''
1371
1403
  const table = longFormat ? longNumberUnits : shortNumberUnits
1372
- const precision = options?.precision ?? 1
1404
+ const precision =
1405
+ (_b = options === null || options === void 0 ? void 0 : options.precision) !== null && _b !== void 0 ? _b : 1
1373
1406
  if (number < thresholds[0]) {
1374
1407
  return `${number}${unitString}`
1375
1408
  }
@@ -1801,7 +1834,7 @@ class Maybe {
1801
1834
  async valueOf() {
1802
1835
  try {
1803
1836
  return await this.value
1804
- } catch {
1837
+ } catch (_a) {
1805
1838
  return null
1806
1839
  }
1807
1840
  }
@@ -1871,19 +1904,33 @@ function filterCoordinates(grid, predicate, direction = 'row-first') {
1871
1904
  }
1872
1905
 
1873
1906
  function isHorizontalLine(tiles, x, y) {
1874
- return tiles[x + 1]?.[y] && tiles[x - 1]?.[y] && !tiles[x][y - 1] && !tiles[x][y + 1]
1907
+ var _a, _b
1908
+ return (
1909
+ ((_a = tiles[x + 1]) === null || _a === void 0 ? void 0 : _a[y]) &&
1910
+ ((_b = tiles[x - 1]) === null || _b === void 0 ? void 0 : _b[y]) &&
1911
+ !tiles[x][y - 1] &&
1912
+ !tiles[x][y + 1]
1913
+ )
1875
1914
  }
1876
1915
 
1877
1916
  function isVerticalLine(tiles, x, y) {
1878
- return tiles[x][y + 1] && tiles[x][y - 1] && !tiles[x - 1]?.[y] && !tiles[x + 1]?.[y]
1917
+ var _a, _b
1918
+ return (
1919
+ tiles[x][y + 1] &&
1920
+ tiles[x][y - 1] &&
1921
+ !((_a = tiles[x - 1]) === null || _a === void 0 ? void 0 : _a[y]) &&
1922
+ !((_b = tiles[x + 1]) === null || _b === void 0 ? void 0 : _b[y])
1923
+ )
1879
1924
  }
1880
1925
 
1881
1926
  function isLeftmost(tiles, x, y) {
1882
- return !tiles[x - 1]?.[y]
1927
+ var _a
1928
+ return !((_a = tiles[x - 1]) === null || _a === void 0 ? void 0 : _a[y])
1883
1929
  }
1884
1930
 
1885
1931
  function isRightmost(tiles, x, y) {
1886
- return !tiles[x + 1]?.[y]
1932
+ var _a
1933
+ return !((_a = tiles[x + 1]) === null || _a === void 0 ? void 0 : _a[y])
1887
1934
  }
1888
1935
 
1889
1936
  function isTopmost(tiles, x, y) {
@@ -1895,18 +1942,19 @@ function isBottommost(tiles, x, y) {
1895
1942
  }
1896
1943
 
1897
1944
  function getCorners(tiles, x, y) {
1945
+ var _a, _b, _c, _d, _e, _f, _g, _h
1898
1946
  const corners = []
1899
1947
  if (!tiles[x][y]) {
1900
- if (tiles[x - 1]?.[y] && tiles[x][y - 1]) {
1948
+ if (((_a = tiles[x - 1]) === null || _a === void 0 ? void 0 : _a[y]) && tiles[x][y - 1]) {
1901
1949
  corners.push({ x, y })
1902
1950
  }
1903
- if (tiles[x + 1]?.[y] && tiles[x][y - 1]) {
1951
+ if (((_b = tiles[x + 1]) === null || _b === void 0 ? void 0 : _b[y]) && tiles[x][y - 1]) {
1904
1952
  corners.push({ x: x + 1, y })
1905
1953
  }
1906
- if (tiles[x - 1]?.[y] && tiles[x][y + 1]) {
1954
+ if (((_c = tiles[x - 1]) === null || _c === void 0 ? void 0 : _c[y]) && tiles[x][y + 1]) {
1907
1955
  corners.push({ x, y: y + 1 })
1908
1956
  }
1909
- if (tiles[x + 1]?.[y] && tiles[x][y + 1]) {
1957
+ if (((_d = tiles[x + 1]) === null || _d === void 0 ? void 0 : _d[y]) && tiles[x][y + 1]) {
1910
1958
  corners.push({ x: x + 1, y: y + 1 })
1911
1959
  }
1912
1960
  return corners
@@ -1914,16 +1962,32 @@ function getCorners(tiles, x, y) {
1914
1962
  if (isHorizontalLine(tiles, x, y) || isVerticalLine(tiles, x, y)) {
1915
1963
  return []
1916
1964
  }
1917
- if (!tiles[x - 1]?.[y - 1] && isLeftmost(tiles, x, y) && isTopmost(tiles, x, y)) {
1965
+ if (
1966
+ !((_e = tiles[x - 1]) === null || _e === void 0 ? void 0 : _e[y - 1]) &&
1967
+ isLeftmost(tiles, x, y) &&
1968
+ isTopmost(tiles, x, y)
1969
+ ) {
1918
1970
  corners.push({ x, y })
1919
1971
  }
1920
- if (!tiles[x + 1]?.[y - 1] && isRightmost(tiles, x, y) && isTopmost(tiles, x, y)) {
1972
+ if (
1973
+ !((_f = tiles[x + 1]) === null || _f === void 0 ? void 0 : _f[y - 1]) &&
1974
+ isRightmost(tiles, x, y) &&
1975
+ isTopmost(tiles, x, y)
1976
+ ) {
1921
1977
  corners.push({ x: x + 1, y })
1922
1978
  }
1923
- if (!tiles[x - 1]?.[y + 1] && isLeftmost(tiles, x, y) && isBottommost(tiles, x, y)) {
1979
+ if (
1980
+ !((_g = tiles[x - 1]) === null || _g === void 0 ? void 0 : _g[y + 1]) &&
1981
+ isLeftmost(tiles, x, y) &&
1982
+ isBottommost(tiles, x, y)
1983
+ ) {
1924
1984
  corners.push({ x, y: y + 1 })
1925
1985
  }
1926
- if (!tiles[x + 1]?.[y + 1] && isRightmost(tiles, x, y) && isBottommost(tiles, x, y)) {
1986
+ if (
1987
+ !((_h = tiles[x + 1]) === null || _h === void 0 ? void 0 : _h[y + 1]) &&
1988
+ isRightmost(tiles, x, y) &&
1989
+ isBottommost(tiles, x, y)
1990
+ ) {
1927
1991
  corners.push({ x: x + 1, y: y + 1 })
1928
1992
  }
1929
1993
  return corners
@@ -1954,22 +2018,28 @@ function findLines(grid, tileSize) {
1954
2018
  grid,
1955
2019
  (x, y) => Boolean(grid[x][y] === 0 && grid[x][y + 1] !== 0),
1956
2020
  'row-first'
1957
- ).map(point => ({ ...point, dx: 1, dy: 0 }))
2021
+ ).map(point => Object.assign(Object.assign({}, point), { dx: 1, dy: 0 }))
1958
2022
  const lowerPoints = filterCoordinates(
1959
2023
  grid,
1960
2024
  (x, y) => Boolean(grid[x][y] === 0 && grid[x][y - 1] !== 0),
1961
2025
  'row-first'
1962
- ).map(point => ({ ...point, dx: 1, dy: 0 }))
2026
+ ).map(point => Object.assign(Object.assign({}, point), { dx: 1, dy: 0 }))
1963
2027
  const rightPoints = filterCoordinates(
1964
2028
  grid,
1965
- (x, y) => Boolean(grid[x][y] === 0 && grid[x - 1]?.[y] !== 0),
2029
+ (x, y) => {
2030
+ var _a
2031
+ return Boolean(grid[x][y] === 0 && ((_a = grid[x - 1]) === null || _a === void 0 ? void 0 : _a[y]) !== 0)
2032
+ },
1966
2033
  'column-first'
1967
- ).map(point => ({ ...point, dx: 0, dy: 1 }))
2034
+ ).map(point => Object.assign(Object.assign({}, point), { dx: 0, dy: 1 }))
1968
2035
  const leftPoints = filterCoordinates(
1969
2036
  grid,
1970
- (x, y) => Boolean(grid[x][y] === 0 && grid[x + 1]?.[y] !== 0),
2037
+ (x, y) => {
2038
+ var _a
2039
+ return Boolean(grid[x][y] === 0 && ((_a = grid[x + 1]) === null || _a === void 0 ? void 0 : _a[y]) !== 0)
2040
+ },
1971
2041
  'column-first'
1972
- ).map(point => ({ ...point, dx: 0, dy: 1 }))
2042
+ ).map(point => Object.assign(Object.assign({}, point), { dx: 0, dy: 1 }))
1973
2043
  upperPoints.forEach(vector => vector.y++)
1974
2044
  leftPoints.forEach(vector => vector.x++)
1975
2045
  const verticalLineSegments = group([...rightPoints, ...leftPoints], (current, previous) => {
@@ -2268,6 +2338,7 @@ exports.Strings = {
2268
2338
  extractBlock,
2269
2339
  extractAllBlocks,
2270
2340
  indexOfEarliest,
2341
+ lastIndexOfBefore,
2271
2342
  parseHtmlAttributes,
2272
2343
  readNextWord,
2273
2344
  resolveVariableWithDefaultSyntax,
@@ -2280,7 +2351,8 @@ exports.Strings = {
2280
2351
  indexOfRegex,
2281
2352
  lineMatches,
2282
2353
  linesMatchInOrder,
2283
- represent
2354
+ represent,
2355
+ resolveMarkdownLinks
2284
2356
  }
2285
2357
 
2286
2358
  exports.Assertions = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "7.0.0",
3
+ "version": "7.2.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {