All files utils.js

93.33% Statements 14/15
77.78% Branches 7/9
83.33% Functions 5/6
100% Lines 14/14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51    7x       3x 77x                           3x 13x 13x                     3x 13x   13x 13x 13x       13x     3x 3x      
class InStorageCacheError extends Error {
  constructor (message, ...args) {
    super(`[InStorageCacheError] ${message}`, ...args)
  }
}
 
const validStorage = storage =>
  Boolean(
    storage &&
      storage.getItem &&
      storage.setItem &&
      storage.removeItem &&
      storage.clear
  )
 
/**
 * Iterates each key of the storage and execute the callback on it.
 *
 * @param {Object} storage The storage instance.
 * @param {Function} callback The iteration callback.
 */
const iterate = (storage, callback) => {
  for (let i = 0; i < storage.length; ++i) {
    callback(storage.key(i))
  }
}
 
/**
 * Creates a plain object from all the storage's persisted data.
 *
 * @param {Object} storage The storage instance.
 * @param {Function} denormalize Method of denormalizing the retrieved resource.
 * @param {String} prefix The storage persisting key prefix.
 */
const toObject = (storage, denormalize = value => value, prefix = '') => {
  const object = {}
 
  iterate(storage, key => {
    Eif (key.indexOf(prefix) === 0) {
      object[key.slice(prefix.length)] = denormalize(storage.getItem(key))
    }
  })
 
  return object
}
 
const normalize = JSON.stringify
const denormalize = JSON.parse
 
export { InStorageCacheError, validStorage, toObject, normalize, denormalize }