11import { 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;
62
73export function nullableAsArray < T > ( value ?: T ) : ReadonlyArray < NonNullable < T > > {
84 if ( value == null ) {
@@ -27,91 +23,18 @@ export function repeatArray<T>(element: T, count: number): ReadonlyArray<T> {
2723export function repeatWithSpace ( text : string , count : number ) : string {
2824 return repeatArray ( text , count ) . join ( " " ) ;
2925}
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- }
8026export function flattenError ( error : unknown ) : ReadonlyArray < unknown > {
8127 if ( error instanceof AggregateError ) {
8228 return error . errors . flatMap ( flattenError ) ;
8329 } else {
8430 return [ error ] ;
8531 }
8632}
87- export function lazy < T > ( fn : ( ) => T ) : ( ) => T {
88- const cache = new Lazy ( fn ) ;
89- return ( ) => cache . getValue ( ) ;
90- }
9133export function deduplicateErrors < T extends Error > (
9234 errors : Iterable < T > ,
9335) : ReadonlyArray < T > {
9436 return distinctBy ( errors , ( { message } ) => message ) ;
9537}
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- }
10438export function throwError ( error : unknown ) : never {
10539 throw error ;
10640}
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