/**
 * Expiration time unit
 * s: seconds
 * m: minute
 * h: hour
 * d: day
 * w: week
 * y: year
 * t: Custom (millisecond)
 */ export type ExpiredUnit = 's' | 'm' | 'h' | 'd' | 'w' | 'y' | 't' ; /**
 * 判断是否在浏览器中渲染Angular应用
 * `decorator` 无法直接使用Angular `PLATFORM_ID` 标识
 * 这带来的好处是当服务端自行填充 Document 时也能很好的识别
 */ export const isBrowser = typeof document === 'object' && ! ! document ; export class StorageUtil { static get ( storage , key ) { if ( storage == null ) { return null ; } const value = StorageUtil . parse ( storage . getItem ( key ) || 'null' ) || null ; if ( value === null ) { return null ; } if ( typeof value === 'object' && typeof value . _expired !== 'undefined' && value . _expired !== 0 && + new Date ( ) > value . _expired ) { StorageUtil . remove ( storage , key ) ; return null ; } return value . _value || null ; } static set ( storage , key , value , expiredAt = 0 , expiredUnit = 't' ) { if ( storage == null ) { return ; } storage . setItem ( key , StorageUtil . stringify ( { _expired : StorageUtil . getExpired ( expiredAt , expiredUnit ) , _value : value , } ) ) ; } static remove ( storage , key ) { if ( storage == null ) { return ; } storage . removeItem ( key ) ; } static key ( storage , index ) { if ( storage == null ) { return null ; } return storage . key ( index ) ; } private static getExpired ( val , unit ) { if ( val <= 0 ) { return 0 ; } const now = + new Date ( ) ; switch ( unit ) { case 's' : return now + 1000 * val ; case 'm' : return now + 1000 * 60 * val ; case 'h' : return now + 1000 * 60 * 60 * val ; case 'd' : return now + 1000 * 60 * 60 * 24 * val ; case 'w' : return now + 1000 * 60 * 60 * 24 * 7 * val ; case 'y' : return now + 1000 * 60 * 60 * 24 * 365 * val ; case 't' : return now + val ; } return 0 ; } private static stringify ( value ) { return JSON . stringify ( value ) ; } private static parse ( text ) { try { return JSON . parse ( text ) || null ; } catch ( e ) { return text ; } } }