1
1
import { distinctBy } from "@std/collections/distinct-by" ;
2
- import { escape } from "@std/regexp/escape" ;
3
- import { Lazy } from "./cache.ts" ;
4
-
5
- export const NEWLINES = / \r ? \n / g;
6
2
7
3
export function nullableAsArray < T > ( value ?: T ) : ReadonlyArray < NonNullable < T > > {
8
4
if ( value == null ) {
@@ -27,91 +23,18 @@ export function repeatArray<T>(element: T, count: number): ReadonlyArray<T> {
27
23
export function repeatWithSpace ( text : string , count : number ) : string {
28
24
return repeatArray ( text , count ) . join ( " " ) ;
29
25
}
30
- export const checkLocalStorage = lazy ( ( ) => {
31
- // https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API
32
- try {
33
- const x = "__storage_test__" ;
34
- localStorage . setItem ( x , x ) ;
35
- localStorage . removeItem ( x ) ;
36
- return true ;
37
- } catch ( e ) {
38
- return e instanceof DOMException &&
39
- e . name === "QuotaExceededError" &&
40
- // acknowledge QuotaExceededError only if there's something already stored
41
- localStorage &&
42
- localStorage . length !== 0 ;
43
- }
44
- } ) ;
45
- export function setIgnoreError ( key : string , value : string ) : void {
46
- if ( checkLocalStorage ( ) ) {
47
- try {
48
- localStorage . setItem ( key , value ) ;
49
- } catch ( error ) {
50
- if (
51
- ! ( error instanceof DOMException ) || error . name !== "QuotaExceededError"
52
- ) {
53
- throw error ;
54
- }
55
- }
56
- }
57
- }
58
- export function assertOk ( response : Response ) : Response {
59
- if ( ! response . ok ) {
60
- const { url, status, statusText } = response ;
61
- throw new Error (
62
- `unable to fetch ${ url } (${ status } ${ statusText } )` ,
63
- ) ;
64
- } else {
65
- return response ;
66
- }
67
- }
68
- export function extractErrorMessage ( error : unknown ) : string {
69
- if ( error instanceof Error ) {
70
- return error . message ;
71
- } else {
72
- return `${ error } ` ;
73
- }
74
- }
75
- export function filterSet < T > (
76
- set : Iterable < readonly [ condition : boolean , value : T ] > ,
77
- ) : ReadonlyArray < T > {
78
- return [ ...set ] . filter ( ( [ condition ] ) => condition ) . map ( ( [ _ , value ] ) => value ) ;
79
- }
80
26
export function flattenError ( error : unknown ) : ReadonlyArray < unknown > {
81
27
if ( error instanceof AggregateError ) {
82
28
return error . errors . flatMap ( flattenError ) ;
83
29
} else {
84
30
return [ error ] ;
85
31
}
86
32
}
87
- export function lazy < T > ( fn : ( ) => T ) : ( ) => T {
88
- const cache = new Lazy ( fn ) ;
89
- return ( ) => cache . getValue ( ) ;
90
- }
91
33
export function deduplicateErrors < T extends Error > (
92
34
errors : Iterable < T > ,
93
35
) : ReadonlyArray < T > {
94
36
return distinctBy ( errors , ( { message } ) => message ) ;
95
37
}
96
- export function characterClass ( characters : Iterable < string > ) : RegExp {
97
- return new RegExp ( `[${ escape ( [ ...characters ] . join ( "" ) ) } ]` , "u" ) ;
98
- }
99
- export function uniquePairs < T > (
100
- array : ReadonlyArray < T > ,
101
- ) : ReadonlyArray < readonly [ T , T ] > {
102
- return array . flatMap ( ( a , i ) => array . slice ( i + 1 ) . map ( ( b ) => [ a , b ] ) ) ;
103
- }
104
38
export function throwError ( error : unknown ) : never {
105
39
throw error ;
106
40
}
107
- export function findDuplicate < T > ( iterable : Iterable < T > ) : null | T {
108
- const set = new Set ( ) ;
109
- for ( const value of iterable ) {
110
- if ( set . has ( value ) ) {
111
- return value ;
112
- } else {
113
- set . add ( value ) ;
114
- }
115
- }
116
- return null ;
117
- }
0 commit comments