/**
 * Konichiwa~
 *
 * This module contains the utility functions that are used
 * by content script, background script and the popup.
 */ import { IGenericObject , ISettings } from "./common" ; param { string } data returns { string } export function dedent ( data ) { return data . split ( "\n" ) . map ( item => item . trim ( ) ) . join ( "" ) ; } param filename returns export function fileSafeString ( filename ) { let re = /[\\/<>*?:"|.]/gi ; return filename . replace ( re , "" ) ; } param name returns export function cleanAnimeName ( name ) { let modifierRe = /\(SUB\)|\(DUB\)/gi ; return name . replace ( modifierRe , "" ) . trim ( ) ; } param num returns export function pad ( num ) { if ( num === "" ) { throw new Error ( "num can't be a blank string" ) ; } if ( num . length >= 3 ) { return num ; } else { return ( "000" + num ) . slice ( - 3 ) ; } } returns export function mergeObject ( obj1 , obj2 ) { for ( let b in obj2 ) { if ( obj2 . hasOwnProperty ( b ) ) { obj1 [ b ] = obj2 [ b ] ; } } return obj1 ; } let parser = document . createElement ( "a" ) ; param uriString returns export function decomposeURL ( uriString ) { parser . href = uriString ; let searchParams = { } ; let url = parser . protocol + "//" + parser . hostname + parser . pathname ; let params = parser . search . slice ( 1 ) ; let items = params . split ( "&" ) ; for ( let item of items ) { let searchSplit = item . split ( "=" ) ; if ( searchSplit [ 0 ] !== "" && searchSplit [ 1 ] !== undefined ) { searchParams [ searchSplit [ 0 ] ] = searchSplit [ 1 ] ; } } return [ url , searchParams ] ; } param url param searchParams export function joinURL ( url , searchParams ) { Object . keys ( searchParams ) . forEach ( ( key , index ) => { if ( index === 0 ) { url += `?${ key }=${ searchParams [ key ] }` ; } else { url += `&${ key }=${ searchParams [ key ] }` ; } } ) ; return url ; } /**
 * A helper function that converts a object to a query string.
 * Credits to [Artur Klesun] https * [Stack Overflow Thread] http *
 * NOTE: Pass flat objects only. Don't pass nested objects.
  export function obj2query ( params ) { return Object . keys ( params ) . map ( k => encodeURIComponent ( k ) + "=" + encodeURIComponent ( params [ k ] . toString ( ) ) ) . join ( "&" ) ; } /**
 * This helper function will help us test whether a given string is a valid URL.
 * Credits to [Daveo] https for the regex.
 * [Stack Overflow Thread] https param url export function isUrl ( url ) { const re = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/ ; return re . test ( url ) ; } export function loadSettings ( key ) { return new Promise ( ( resolve , reject ) => { chrome . storage . local . get ( key , ( result ) => { resolve ( result ) ; } ) ; } ) ; }