1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 09:46:22 +00:00

extract renameStorage, removeStorage methods

and tests for each of them
This commit is contained in:
Dick Choi
2016-08-26 10:26:22 +09:00
parent eacd01e77e
commit c5414aadd1
6 changed files with 144 additions and 1 deletions

View File

@@ -0,0 +1,33 @@
const _ = require('lodash')
/**
* @param {String} key
* @param {String} name
* @return {Object} Storage meta data
*/
function renameStorage (key, name) {
if (!_.isString(name)) return Promise.reject(new Error('Name must be a string.'))
let rawStorages
try {
rawStorages = JSON.parse(localStorage.getItem('storages'))
if (!_.isArray(rawStorages)) throw new Error('invalid storages')
} catch (e) {
console.warn(e)
rawStorages = []
}
let targetStorage
for (let i = 0; i < rawStorages.length; i++) {
if (rawStorages[i].key === key) {
rawStorages[i].name = name
targetStorage = rawStorages[i]
}
}
localStorage.setItem('storages', JSON.stringify(rawStorages))
return Promise.resolve(targetStorage)
}
module.exports = renameStorage