Rename valueOf to fromJSON

jh-changes
Shin'ya UEOKA 5 years ago
parent 16352502cf
commit 532eeb5a1d
  1. 2
      src/background/repositories/PersistentSettingRepository.ts
  2. 2
      src/settings/components/form/KeymapsForm.tsx
  3. 4
      src/settings/components/form/SearchForm.tsx
  4. 2
      src/settings/components/index.tsx
  5. 2
      src/settings/storage.ts
  6. 24
      src/shared/SettingData.ts
  7. 4
      test/settings/components/form/KeymapsForm.test.tsx
  8. 8
      test/settings/components/form/SearchEngineForm.test.tsx
  9. 12
      test/shared/SettingData.test.ts

@ -8,7 +8,7 @@ export default class SettingRepository {
if (!settings) { if (!settings) {
return null; return null;
} }
return SettingData.valueOf(settings as any); return SettingData.fromJSON(settings as any);
} }
} }

@ -12,7 +12,7 @@ interface Props {
class KeymapsForm extends React.Component<Props> { class KeymapsForm extends React.Component<Props> {
public static defaultProps: Props = { public static defaultProps: Props = {
value: FormKeymaps.valueOf({}), value: FormKeymaps.fromJSON({}),
onChange: () => {}, onChange: () => {},
onBlur: () => {}, onBlur: () => {},
}; };

@ -12,7 +12,7 @@ interface Props {
class SearchForm extends React.Component<Props> { class SearchForm extends React.Component<Props> {
public static defaultProps: Props = { public static defaultProps: Props = {
value: FormSearch.valueOf({ default: '', engines: []}), value: FormSearch.fromJSON({ default: '', engines: []}),
onChange: () => {}, onChange: () => {},
onBlur: () => {}, onBlur: () => {},
}; };
@ -81,7 +81,7 @@ class SearchForm extends React.Component<Props> {
} }
} }
this.props.onChange(FormSearch.valueOf(next)); this.props.onChange(FormSearch.fromJSON(next));
if (name === 'delete' || name === 'default') { if (name === 'delete' || name === 'default') {
this.props.onBlur(); this.props.onBlur();
} }

@ -133,7 +133,7 @@ class SettingsComponent extends React.Component<Props> {
let data = new SettingData({ let data = new SettingData({
source: this.props.source, source: this.props.source,
form: (this.props.form as FormSettings).buildWithSearch( form: (this.props.form as FormSettings).buildWithSearch(
FormSearch.valueOf(value)), FormSearch.fromJSON(value)),
}); });
this.props.dispatch(settingActions.set(data)); this.props.dispatch(settingActions.set(data));
} }

@ -6,7 +6,7 @@ export const load = async(): Promise<SettingData> => {
return DefaultSettingData; return DefaultSettingData;
} }
try { try {
return SettingData.valueOf(settings as any); return SettingData.fromJSON(settings as any);
} catch (e) { } catch (e) {
console.error('unable to load settings', e); console.error('unable to load settings', e);
return DefaultSettingData; return DefaultSettingData;

@ -6,9 +6,9 @@ import Properties from './settings/Properties';
import Blacklist from './settings/Blacklist'; import Blacklist from './settings/Blacklist';
export class FormKeymaps { export class FormKeymaps {
private data: {[op: string]: string}; private readonly data: {[op: string]: string};
constructor(data: {[op: string]: string}) { private constructor(data: {[op: string]: string}) {
this.data = data; this.data = data;
} }
@ -38,7 +38,7 @@ export class FormKeymaps {
return new FormKeymaps(newData); return new FormKeymaps(newData);
} }
static valueOf(o: ReturnType<FormKeymaps['toJSON']>): FormKeymaps { static fromJSON(o: ReturnType<FormKeymaps['toJSON']>): FormKeymaps {
let data: {[op: string]: string} = {}; let data: {[op: string]: string} = {};
for (let op of Object.keys(o)) { for (let op of Object.keys(o)) {
data[op] = o[op] as string; data[op] = o[op] as string;
@ -65,9 +65,9 @@ export class FormKeymaps {
} }
export class FormSearch { export class FormSearch {
private default: string; private readonly default: string;
private engines: string[][]; private readonly engines: string[][];
constructor(defaultEngine: string, engines: string[][]) { constructor(defaultEngine: string, engines: string[][]) {
this.default = defaultEngine; this.default = defaultEngine;
@ -92,7 +92,7 @@ export class FormSearch {
}; };
} }
static valueOf(o: ReturnType<FormSearch['toJSON']>): FormSearch { static fromJSON(o: ReturnType<FormSearch['toJSON']>): FormSearch {
if (!Object.prototype.hasOwnProperty.call(o, 'default')) { if (!Object.prototype.hasOwnProperty.call(o, 'default')) {
throw new TypeError(`"default" field not set`); throw new TypeError(`"default" field not set`);
} }
@ -220,15 +220,15 @@ export class FormSettings {
}; };
} }
static valueOf(o: ReturnType<FormSettings['toJSON']>): FormSettings { static fromJSON(o: ReturnType<FormSettings['toJSON']>): FormSettings {
for (let name of ['keymaps', 'search', 'properties', 'blacklist']) { for (let name of ['keymaps', 'search', 'properties', 'blacklist']) {
if (!Object.prototype.hasOwnProperty.call(o, name)) { if (!Object.prototype.hasOwnProperty.call(o, name)) {
throw new Error(`"${name}" field not set`); throw new Error(`"${name}" field not set`);
} }
} }
return new FormSettings( return new FormSettings(
FormKeymaps.valueOf(o.keymaps), FormKeymaps.fromJSON(o.keymaps),
FormSearch.valueOf(o.search), FormSearch.fromJSON(o.search),
Properties.fromJSON(o.properties), Properties.fromJSON(o.properties),
Blacklist.fromJSON(o.blacklist), Blacklist.fromJSON(o.blacklist),
); );
@ -311,7 +311,7 @@ export default class SettingData {
throw new Error(`unknown settings source: ${this.source}`); throw new Error(`unknown settings source: ${this.source}`);
} }
static valueOf(o: { static fromJSON(o: {
source: string; source: string;
json?: string; json?: string;
form?: ReturnType<FormSettings['toJSON']>; form?: ReturnType<FormSettings['toJSON']>;
@ -326,7 +326,7 @@ export default class SettingData {
case SettingSource.Form: case SettingSource.Form:
return new SettingData({ return new SettingData({
source: o.source, source: o.source,
form: FormSettings.valueOf( form: FormSettings.fromJSON(
o.form as ReturnType<FormSettings['toJSON']>), o.form as ReturnType<FormSettings['toJSON']>),
}); });
} }
@ -334,7 +334,7 @@ export default class SettingData {
} }
} }
export const DefaultSettingData: SettingData = SettingData.valueOf({ export const DefaultSettingData: SettingData = SettingData.fromJSON({
source: 'json', source: 'json',
json: DefaultSettingJSONText, json: DefaultSettingJSONText,
}); });

@ -9,7 +9,7 @@ import { expect } from 'chai';
describe("settings/form/KeymapsForm", () => { describe("settings/form/KeymapsForm", () => {
describe('render', () => { describe('render', () => {
it('renders keymap fields', () => { it('renders keymap fields', () => {
let root = ReactTestRenderer.create(<KeymapsForm value={FormKeymaps.valueOf({ let root = ReactTestRenderer.create(<KeymapsForm value={FormKeymaps.fromJSON({
'scroll.vertically?{"count":1}': 'j', 'scroll.vertically?{"count":1}': 'j',
'scroll.vertically?{"count":-1}': 'k', 'scroll.vertically?{"count":-1}': 'k',
})} />).root })} />).root
@ -48,7 +48,7 @@ describe("settings/form/KeymapsForm", () => {
it('invokes onChange event on edit', (done) => { it('invokes onChange event on edit', (done) => {
ReactTestUtils.act(() => { ReactTestUtils.act(() => {
ReactDOM.render(<KeymapsForm ReactDOM.render(<KeymapsForm
value={FormKeymaps.valueOf({ value={FormKeymaps.fromJSON({
'scroll.vertically?{"count":1}': 'j', 'scroll.vertically?{"count":1}': 'j',
'scroll.vertically?{"count":-1}': 'k', 'scroll.vertically?{"count":-1}': 'k',
})} })}

@ -8,7 +8,7 @@ import { FormSearch } from 'shared/SettingData';
describe("settings/form/SearchForm", () => { describe("settings/form/SearchForm", () => {
describe('render', () => { describe('render', () => {
it('renders SearchForm', () => { it('renders SearchForm', () => {
let root = ReactTestRenderer.create(<SearchForm value={FormSearch.valueOf({ let root = ReactTestRenderer.create(<SearchForm value={FormSearch.fromJSON({
default: 'google', default: 'google',
engines: [['google', 'google.com'], ['yahoo', 'yahoo.com']], engines: [['google', 'google.com'], ['yahoo', 'yahoo.com']],
})} />).root; })} />).root;
@ -41,7 +41,7 @@ describe("settings/form/SearchForm", () => {
it('invokes onChange event on edit', (done) => { it('invokes onChange event on edit', (done) => {
ReactTestUtils.act(() => { ReactTestUtils.act(() => {
ReactDOM.render(<SearchForm ReactDOM.render(<SearchForm
value={FormSearch.valueOf({ value={FormSearch.fromJSON({
default: 'google', default: 'google',
engines: [['google', 'google.com'], ['yahoo', 'yahoo.com']] engines: [['google', 'google.com'], ['yahoo', 'yahoo.com']]
})} })}
@ -67,7 +67,7 @@ describe("settings/form/SearchForm", () => {
it('invokes onChange event on delete', (done) => { it('invokes onChange event on delete', (done) => {
ReactTestUtils.act(() => { ReactTestUtils.act(() => {
ReactDOM.render(<SearchForm value={FormSearch.valueOf({ ReactDOM.render(<SearchForm value={FormSearch.fromJSON({
default: 'yahoo', default: 'yahoo',
engines: [['louvre', 'google.com'], ['yahoo', 'yahoo.com']] engines: [['louvre', 'google.com'], ['yahoo', 'yahoo.com']]
})} })}
@ -88,7 +88,7 @@ describe("settings/form/SearchForm", () => {
it('invokes onChange event on add', (done) => { it('invokes onChange event on add', (done) => {
ReactTestUtils.act(() => { ReactTestUtils.act(() => {
ReactDOM.render(<SearchForm value={FormSearch.valueOf({ ReactDOM.render(<SearchForm value={FormSearch.fromJSON({
default: 'yahoo', default: 'yahoo',
engines: [['google', 'google.com']] engines: [['google', 'google.com']]
})} })}

@ -14,7 +14,7 @@ describe('shared/SettingData', () => {
'scroll.home': '0', 'scroll.home': '0',
}; };
let keymaps = FormKeymaps.valueOf(data).toKeymaps().toJSON(); let keymaps = FormKeymaps.fromJSON(data).toKeymaps().toJSON();
expect(keymaps).to.deep.equal({ expect(keymaps).to.deep.equal({
'j': { type: 'scroll.vertically', count: 1 }, 'j': { type: 'scroll.vertically', count: 1 },
'0': { type: 'scroll.home' }, '0': { type: 'scroll.home' },
@ -118,7 +118,7 @@ describe('shared/SettingData', () => {
blacklist: [] blacklist: []
}; };
let settings = FormSettings.valueOf(data).toSettings(); let settings = FormSettings.fromJSON(data).toSettings();
expect({ expect({
keymaps: settings.keymaps.toJSON(), keymaps: settings.keymaps.toJSON(),
search: settings.search.toJSON(), search: settings.search.toJSON(),
@ -211,7 +211,7 @@ describe('shared/SettingData', () => {
}`, }`,
}; };
let j = SettingData.valueOf(data).toJSON(); let j = SettingData.fromJSON(data).toJSON();
expect(j.source).to.equal('json'); expect(j.source).to.equal('json');
expect(j.json).to.be.a('string'); expect(j.json).to.be.a('string');
}); });
@ -236,7 +236,7 @@ describe('shared/SettingData', () => {
}, },
}; };
let j = SettingData.valueOf(data).toJSON(); let j = SettingData.fromJSON(data).toJSON();
expect(j.source).to.equal('form'); expect(j.source).to.equal('form');
expect(j.form).to.deep.equal({ expect(j.form).to.deep.equal({
keymaps: {}, keymaps: {},
@ -277,7 +277,7 @@ describe('shared/SettingData', () => {
}`, }`,
}; };
let settings = SettingData.valueOf(data).toSettings(); let settings = SettingData.fromJSON(data).toSettings();
expect(settings.search.defaultEngine).to.equal('google'); expect(settings.search.defaultEngine).to.equal('google');
}); });
@ -301,7 +301,7 @@ describe('shared/SettingData', () => {
}, },
}; };
let settings = SettingData.valueOf(data).toSettings(); let settings = SettingData.fromJSON(data).toSettings();
expect(settings.search.defaultEngine).to.equal('yahoo'); expect(settings.search.defaultEngine).to.equal('yahoo');
}); });
}); });