From d181b84c3b64bc7deb7d7e759b16f5d2cfafec09 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Wed, 19 Aug 2020 17:43:57 +0300 Subject: [PATCH] fix(cloudcmd) client: edit: json files (#294) --- .putout.json | 5 ++- client/client.js | 6 +-- client/dom/storage.js | 16 +++++--- client/dom/storage.spec.js | 75 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 10 deletions(-) create mode 100644 client/dom/storage.spec.js diff --git a/.putout.json b/.putout.json index 9a3ab59f..6fcdff2e 100644 --- a/.putout.json +++ b/.putout.json @@ -27,7 +27,10 @@ "apply-shorthand-properties": "off" }, "test/common/cloudfunc.js": { - "remove-console": false + "remove-console": "off" + }, + "storage.js": { + "remove-useless-async": "off" } } } diff --git a/client/client.js b/client/client.js index c553b1c6..d30d83bb 100644 --- a/client/client.js +++ b/client/client.js @@ -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); } /** diff --git a/client/dom/storage.js b/client/dom/storage.js index a2de5201..bfef931b 100644 --- a/client/dom/storage.js +++ b/client/dom/storage.js @@ -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); diff --git a/client/dom/storage.spec.js b/client/dom/storage.spec.js new file mode 100644 index 00000000..3922a616 --- /dev/null +++ b/client/dom/storage.spec.js @@ -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(); +});