fix(cloudcmd) client: edit: json files (#294)

This commit is contained in:
coderaiser 2020-08-19 17:43:57 +03:00
parent 5732752350
commit d181b84c3b
4 changed files with 92 additions and 10 deletions

View file

@ -268,7 +268,7 @@ function CloudCmdProto(DOM) {
const data = await Storage.get(dirPath);
if (!data)
await Storage.set(dirPath, getJsonFromFileTable());
await Storage.setJson(dirPath, getJsonFromFileTable());
}
function getPanels() {
@ -328,7 +328,7 @@ function CloudCmdProto(DOM) {
CloudCmd.log('reading dir: "' + path + '";');
const dirStorage = CloudCmd.config('dirStorage');
const json = dirStorage && await Storage.get(path);
const json = dirStorage && await Storage.getJson(path);
const name = options.currentName || Info.name;
const {
@ -364,7 +364,7 @@ function CloudCmdProto(DOM) {
if (!CloudCmd.config('dirStorage'))
return;
Storage.set(path, newObj);
Storage.setJson(path, newObj);
}
/**

View file

@ -1,17 +1,21 @@
'use strict';
const tryCatch = require('try-catch');
const {parse, stringify} = JSON;
const isObj = (a) => typeof a === 'object';
module.exports.set = (name, data) => {
const primitive = !isObj(data) ? data : stringify(data);
localStorage.setItem(name, primitive);
module.exports.set = async (name, data) => {
localStorage.setItem(name, data);
};
module.exports.setJson = async (name, data) => {
localStorage.setItem(name, stringify(data));
};
module.exports.get = async (name) => {
return localStorage.getItem(name);
};
module.exports.getJson = async (name) => {
const data = localStorage.getItem(name);
const [, result = data] = tryCatch(parse, data);

View file

@ -0,0 +1,75 @@
'use strict';
const test = require('supertape');
const stub = require('@cloudcmd/stub');
const storage = require('./storage');
const {stringify} = JSON;
test('cloudcmd: client: storage: set', async (t) => {
const {localStorage} = global;
const setItem = stub();
global.localStorage = {
setItem,
};
await storage.set('hello', 'world');
global.localStorage = localStorage;
t.ok(setItem.calledWith('hello', 'world'), 'should call setItem');
t.end();
});
test('cloudcmd: client: storage: get', async (t) => {
const {localStorage} = global;
const getItem = stub().returns('world');
global.localStorage = {
getItem,
};
const result = await storage.get('hello');
global.localStorage = localStorage;
t.equal(result, 'world');
t.end();
});
test('cloudcmd: client: storage: getJson', async (t) => {
const {localStorage} = global;
const expected = {
hello: 'world',
};
const getItem = stub().returns(stringify(expected));
global.localStorage = {
getItem,
};
const result = await storage.getJson('hello');
global.localStorage = localStorage;
t.deepEqual(result, expected);
t.end();
});
test('cloudcmd: client: storage: setJson', async (t) => {
const {localStorage} = global;
const data = {
hello: 'world',
};
const expected = stringify(data);
const setItem = stub();
global.localStorage = {
setItem,
};
await storage.setJson('hello', data);
global.localStorage = localStorage;
t.ok(setItem.calledWith('hello', expected));
t.end();
});