settings helpers

jh-changes
Shin'ya Ueoka 7 years ago
parent f27d21908a
commit d15de42a75
  1. 1
      .eslintrc
  2. 4
      src/settings/actions/setting.js
  3. 91
      src/shared/settings/values.js
  4. 112
      test/shared/settings/values.test.js

@ -44,6 +44,7 @@
"no-plusplus": "off", "no-plusplus": "off",
"no-ternary": "off", "no-ternary": "off",
"no-undefined": "off", "no-undefined": "off",
"no-undef-init": "off",
"no-unused-vars": ["error", { "varsIgnorePattern": "h" }], "no-unused-vars": ["error", { "varsIgnorePattern": "h" }],
"no-use-before-define": "off", "no-use-before-define": "off",
"no-warning-comments": "off", "no-warning-comments": "off",

@ -27,9 +27,9 @@ const save = (settings) => {
const set = (settings) => { const set = (settings) => {
let value = JSON.parse(DefaultSettings.json); let value = JSON.parse(DefaultSettings.json);
if (settings.source === 'json') { if (settings.source === 'json') {
value = settingsValues.fromJson(settings.json); value = settingsValues.valueFromJson(settings.json);
} else if (settings.source === 'form') { } else if (settings.source === 'form') {
value = settingsValues.fromForm(settings.form); value = settingsValues.valueFromForm(settings.form);
} }
return { return {

@ -1,4 +1,6 @@
const operationFromName = (name) => { import DefaultSettings from './default';
const operationFromFormName = (name) => {
let [type, argStr] = name.split('?'); let [type, argStr] = name.split('?');
let args = {}; let args = {};
if (argStr) { if (argStr) {
@ -7,29 +9,92 @@ const operationFromName = (name) => {
return Object.assign({ type }, args); return Object.assign({ type }, args);
}; };
const fromJson = (json) => { const operationToFormName = (op) => {
let type = op.type;
let args = Object.assign({}, op);
delete args.type;
if (Object.keys(args).length === 0) {
return type;
}
return op.type + '?' + JSON.stringify(args);
};
const valueFromJson = (json) => {
return JSON.parse(json); return JSON.parse(json);
}; };
const fromForm = (form) => { const valueFromForm = (form) => {
let keymaps = {}; let keymaps = undefined;
if (form.keymaps) {
keymaps = {};
for (let name of Object.keys(form.keymaps)) { for (let name of Object.keys(form.keymaps)) {
let keys = form.keymaps[name]; let keys = form.keymaps[name];
keymaps[keys] = operationFromName(name); keymaps[keys] = operationFromFormName(name);
}
} }
let engines = {}; let search = undefined;
for (let { name, url } of form.search.engines) { if (form.search) {
engines[name] = url; search = { default: form.search.default };
if (form.search.engines) {
search.engines = {};
for (let [name, url] of form.search.engines) {
search.engines[name] = url;
}
}
} }
let search = {
default: form.search.default,
engines,
};
let blacklist = form.blacklist; let blacklist = form.blacklist;
return { keymaps, search, blacklist }; return { keymaps, search, blacklist };
}; };
export { fromJson, fromForm }; const jsonFromValue = (value) => {
return JSON.stringify(value, undefined, 2);
};
const formFromValue = (value) => {
let keymaps = undefined;
if (value.keymaps) {
let allowedOps = new Set(Object.keys(DefaultSettings.form.keymaps));
keymaps = {};
for (let keys of Object.keys(value.keymaps)) {
let op = operationToFormName(value.keymaps[keys]);
if (allowedOps.has(op)) {
keymaps[op] = keys;
}
}
}
let search = undefined;
if (value.search) {
search = { default: value.search.default };
if (value.search.engines) {
search.engines = Object.keys(value.search.engines).map((name) => {
return [name, value.search.engines[name]];
});
}
}
let blacklist = value.blacklist;
return { keymaps, search, blacklist };
};
const jsonFromForm = (form) => {
return jsonFromValue(valueFromForm(form));
};
const formFromJson = (json) => {
let value = valueFromJson(json);
return formFromValue(value);
};
export {
valueFromJson, valueFromForm, jsonFromValue, formFromValue,
jsonFromForm, formFromJson
};

@ -0,0 +1,112 @@
import { expect } from 'chai';
import * as values from 'shared/settings/values';
describe("settings values", () => {
describe('valueFromJson', () => {
it('return object from json string', () => {
let json = `{
"keymaps": { "0": {"type": "scroll.home"}},
"search": { "default": "google", "engines": { "google": "https://google.com/search?q={}" }},
"blacklist": [ "*.slack.com"]
}`;
let value = values.valueFromJson(json);
expect(value.keymaps).to.deep.equal({ 0: {type: "scroll.home"}});
expect(value.search).to.deep.equal({ default: "google", engines: { google: "https://google.com/search?q={}"} });
expect(value.blacklist).to.deep.equal(["*.slack.com"]);
});
});
describe('valueFromForm', () => {
it('returns value from form', () => {
let form = {
keymaps: {
'scroll.vertically?{"count":1}': 'j',
'scroll.home': '0',
},
search: {
default: 'google',
engines: [['google', 'https://google.com/search?q={}']],
},
blacklist: ['*.slack.com'],
};
let value = values.valueFromForm(form);
expect(value.keymaps).to.have.deep.property('j', { type: "scroll.vertically", count: 1 });
expect(value.keymaps).to.have.deep.property('0', { type: "scroll.home" });
expect(JSON.stringify(value.search)).to.deep.equal(JSON.stringify({ default: "google", engines: { google: "https://google.com/search?q={}"} }));
expect(value.search).to.deep.equal({ default: "google", engines: { google: "https://google.com/search?q={}"} });
expect(value.blacklist).to.deep.equal(["*.slack.com"]);
});
it('convert from empty form', () => {
let form = {};
let value = values.valueFromForm(form);
expect(value).to.not.have.key('keymaps');
expect(value).to.not.have.key('search');
expect(value).to.not.have.key('blacklist');
});
it('override keymaps', () => {
let form = {
keymaps: {
'scroll.vertically?{"count":1}': 'j',
'scroll.vertically?{"count":-1}': 'j',
}
};
let value = values.valueFromForm(form);
expect(value.keymaps).to.have.key('j');
});
it('override search engine', () => {
let form = {
search: {
default: 'google',
engines: [
['google', 'https://google.com/search?q={}'],
['google', 'https://google.co.jp/search?q={}'],
]
}
};
let value = values.valueFromForm(form);
expect(value.search.engines).to.have.property('google', 'https://google.co.jp/search?q={}');
});
});
describe('jsonFromValue', () => {
});
describe('formFromValue', () => {
it('convert empty value to form', () => {
let value = {};
let form = values.formFromValue(value);
expect(value).to.not.have.key('keymaps');
expect(value).to.not.have.key('search');
expect(value).to.not.have.key('blacklist');
});
it('convert value to form', () => {
let value = {
keymaps: {
j: { type: 'scroll.vertically', count: 1 },
JJ: { type: 'scroll.vertically', count: 100 },
0: { type: 'scroll.home' },
},
search: { default: 'google', engines: { google: 'https://google.com/search?q={}' }},
blacklist: [ '*.slack.com']
};
let form = values.formFromValue(value);
expect(form.keymaps).to.have.property('scroll.vertically?{"count":1}', 'j');
expect(form.keymaps).to.have.property('scroll.home', '0');
expect(Object.keys(form.keymaps)).to.have.lengthOf(2);
expect(form.search).to.have.property('default', 'google');
expect(form.search).to.have.deep.property('engines', [['google', 'https://google.com/search?q={}']]);
expect(form.blacklist).to.have.lengthOf(1);
expect(form.blacklist).to.include('*.slack.com');
});
});
});