cafe-utility 18.2.0 → 18.4.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 +9 -0
  2. package/index.js +37 -1
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -84,6 +84,7 @@ declare function asTime(value: any): string;
84
84
  declare function asArray(value: any): unknown[];
85
85
  declare function asObject(value: any): Record<string, unknown>;
86
86
  declare function asNumericDictionary(value: any): Record<string, number>;
87
+ declare function asUrl(value: any): string;
87
88
  declare function isNullable(typeFn: (value: any) => boolean, value: any): boolean;
88
89
  declare function asNullable<T>(typeFn: (value: any) => T, value: any): T | null;
89
90
  declare function enforceObjectShape(value: Record<string, any>, shape: Record<string, (value: any) => boolean>): boolean;
@@ -199,6 +200,11 @@ declare function hashCode(string: string): number;
199
200
  declare function replaceWord(string: string, search: string, replace: string, whitespaceOnly?: boolean): string;
200
201
  declare function replacePascalCaseWords(string: string, replacer: (word: string) => string): string;
201
202
  declare function stripHtml(string: string): string;
203
+ declare function breakLine(string: string): {
204
+ line: string;
205
+ rest: string;
206
+ };
207
+ declare function toLines(string: string, maxWidth: number, characterWidths?: Record<string, number>): string[];
202
208
  declare function containsWord(string: string, word: string): boolean;
203
209
  declare function containsWords(string: string, words: string[], mode: 'any' | 'all'): boolean;
204
210
  declare function parseHtmlAttributes(string: string): Record<string, string>;
@@ -618,6 +624,7 @@ export declare const Types: {
618
624
  asArray: typeof asArray;
619
625
  asObject: typeof asObject;
620
626
  asNumericDictionary: typeof asNumericDictionary;
627
+ asUrl: typeof asUrl;
621
628
  asNullable: typeof asNullable;
622
629
  enforceObjectShape: typeof enforceObjectShape;
623
630
  enforceArrayShape: typeof enforceArrayShape;
@@ -709,6 +716,8 @@ export declare const Strings: {
709
716
  replaceWord: typeof replaceWord;
710
717
  replacePascalCaseWords: typeof replacePascalCaseWords;
711
718
  stripHtml: typeof stripHtml;
719
+ breakLine: typeof breakLine;
720
+ toLines: typeof toLines;
712
721
  };
713
722
  export declare const Assertions: {
714
723
  asEqual: typeof asEqual;
package/index.js CHANGED
@@ -430,6 +430,13 @@ function asNumericDictionary(n) {
430
430
  if (!e.every(isString) || !r.every(isNumber)) throw new TypeError('Expected numeric dictionary, got: ' + n)
431
431
  return t
432
432
  }
433
+ function isUrl(n) {
434
+ return isString(n) && n.match(/^https?:\/\/.+/) !== null
435
+ }
436
+ function asUrl(n) {
437
+ if (!isUrl(n)) throw new TypeError('Expected url, got: ' + n)
438
+ return n
439
+ }
433
440
  function isNullable(n, t) {
434
441
  return isUndefined(t) || t === null ? !0 : n(t)
435
442
  }
@@ -1053,6 +1060,32 @@ function replacePascalCaseWords(n, t) {
1053
1060
  function stripHtml(n) {
1054
1061
  return n.replace(/<[^>]*>/g, '')
1055
1062
  }
1063
+ function breakLine(n) {
1064
+ const t = n.lastIndexOf(' ')
1065
+ if (t === -1) return { line: n, rest: '' }
1066
+ const e = n.slice(0, t),
1067
+ r = n.slice(t + 1)
1068
+ return { line: e, rest: r }
1069
+ }
1070
+ function toLines(n, t, e = {}) {
1071
+ const r = []
1072
+ let o = '',
1073
+ i = 0
1074
+ for (let s = 0; s < n.length; s++) {
1075
+ const c = n[s],
1076
+ u = e[c] || 1
1077
+ if (((o += c), (i += u), i > t)) {
1078
+ const { line: l, rest: f } = breakLine(o)
1079
+ r.push(l),
1080
+ (o = f),
1081
+ (i = f
1082
+ .split('')
1083
+ .map(a => e[a] || 1)
1084
+ .reduce((a, h) => a + h, 0))
1085
+ }
1086
+ }
1087
+ return o && r.push(o), r
1088
+ }
1056
1089
  function containsWord(n, t) {
1057
1090
  return new RegExp(`\\b${t}\\b`).test(n)
1058
1091
  }
@@ -2226,6 +2259,7 @@ function raycastCircle(n, t, e) {
2226
2259
  asArray,
2227
2260
  asObject,
2228
2261
  asNumericDictionary,
2262
+ asUrl,
2229
2263
  asNullable,
2230
2264
  enforceObjectShape,
2231
2265
  enforceArrayShape
@@ -2316,7 +2350,9 @@ function raycastCircle(n, t, e) {
2316
2350
  hashCode,
2317
2351
  replaceWord,
2318
2352
  replacePascalCaseWords,
2319
- stripHtml
2353
+ stripHtml,
2354
+ breakLine,
2355
+ toLines
2320
2356
  }),
2321
2357
  (exports.Assertions = { asEqual, asTrue, asTruthy, asFalse, asFalsy, asEither }),
2322
2358
  (exports.Cache = { get: getCached }),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "18.2.0",
3
+ "version": "18.4.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {