From 410ffbb0376b9399928ef8d4dd13079bfb120e14 Mon Sep 17 00:00:00 2001 From: Shin'ya UEOKA Date: Fri, 4 Oct 2019 04:01:35 +0000 Subject: Make Keymap class --- src/settings/actions/index.ts | 8 ++++---- src/settings/actions/setting.ts | 6 +++--- src/settings/components/index.tsx | 12 ++++++------ src/settings/reducers/setting.ts | 6 +++--- 4 files changed, 16 insertions(+), 16 deletions(-) (limited to 'src/settings') diff --git a/src/settings/actions/index.ts b/src/settings/actions/index.ts index b1e996e..dfa41c4 100644 --- a/src/settings/actions/index.ts +++ b/src/settings/actions/index.ts @@ -1,5 +1,5 @@ import { - JSONSettings, FormSettings, SettingSource, + JSONTextSettings, FormSettings, SettingSource, } from '../../shared/SettingData'; // Settings @@ -11,14 +11,14 @@ export const SETTING_SWITCH_TO_JSON = 'setting.switch.to.json'; interface SettingSetSettingsAcion { type: typeof SETTING_SET_SETTINGS; source: SettingSource; - json?: JSONSettings; + json?: JSONTextSettings; form?: FormSettings; } interface SettingShowErrorAction { type: typeof SETTING_SHOW_ERROR; error: string; - json: JSONSettings; + json: JSONTextSettings; } interface SettingSwitchToFormAction { @@ -28,7 +28,7 @@ interface SettingSwitchToFormAction { interface SettingSwitchToJsonAction { type: typeof SETTING_SWITCH_TO_JSON; - json: JSONSettings, + json: JSONTextSettings, } export type SettingAction = diff --git a/src/settings/actions/setting.ts b/src/settings/actions/setting.ts index 9eb416e..9404791 100644 --- a/src/settings/actions/setting.ts +++ b/src/settings/actions/setting.ts @@ -1,7 +1,7 @@ import * as actions from './index'; import * as storages from '../storage'; import SettingData, { - JSONSettings, FormSettings, SettingSource, + JSONTextSettings, FormSettings, SettingSource, } from '../../shared/SettingData'; const load = async(): Promise => { @@ -26,7 +26,7 @@ const save = async(data: SettingData): Promise => { return set(data); }; -const switchToForm = (json: JSONSettings): actions.SettingAction => { +const switchToForm = (json: JSONTextSettings): actions.SettingAction => { try { // toSettings exercise validation let form = FormSettings.fromSettings(json.toSettings()); @@ -44,7 +44,7 @@ const switchToForm = (json: JSONSettings): actions.SettingAction => { }; const switchToJson = (form: FormSettings): actions.SettingAction => { - let json = JSONSettings.fromSettings(form.toSettings()); + let json = JSONTextSettings.fromSettings(form.toSettings()); return { type: actions.SETTING_SWITCH_TO_JSON, json, diff --git a/src/settings/components/index.tsx b/src/settings/components/index.tsx index eeac2cf..ada6efb 100644 --- a/src/settings/components/index.tsx +++ b/src/settings/components/index.tsx @@ -8,7 +8,7 @@ import BlacklistForm from './form/BlacklistForm'; import PropertiesForm from './form/PropertiesForm'; import * as settingActions from '../../settings/actions/setting'; import SettingData, { - JSONSettings, FormKeymaps, FormSearch, FormSettings, + JSONTextSettings, FormKeymaps, FormSearch, FormSettings, } from '../../shared/SettingData'; import { State as AppState } from '../reducers/setting'; import * as settings from '../../shared/Settings'; @@ -75,7 +75,7 @@ class SettingsComponent extends React.Component { ; } - renderJsonFields(json: JSONSettings, error: string) { + renderJsonFields(json: JSONTextSettings, error: string) { return
{ error={error} onValueChange={this.bindJson.bind(this)} onBlur={this.save.bind(this)} - value={json.toJSON()} + value={json.toJSONText()} />
; } @@ -97,7 +97,7 @@ class SettingsComponent extends React.Component { fields = this.renderFormFields(this.props.form); } else if (this.props.source === 'json') { fields = this.renderJsonFields( - this.props.json as JSONSettings, this.props.error); + this.props.json as JSONTextSettings, this.props.error); } return (
@@ -165,7 +165,7 @@ class SettingsComponent extends React.Component { bindJson(_name: string, value: string) { let data = new SettingData({ source: this.props.source, - json: JSONSettings.valueOf(value), + json: JSONTextSettings.fromText(value), }); this.props.dispatch(settingActions.set(data)); } @@ -183,7 +183,7 @@ class SettingsComponent extends React.Component { return; } this.props.dispatch( - settingActions.switchToForm(this.props.json as JSONSettings)); + settingActions.switchToForm(this.props.json as JSONTextSettings)); this.save(); } } diff --git a/src/settings/reducers/setting.ts b/src/settings/reducers/setting.ts index c4a21c7..89bf1cb 100644 --- a/src/settings/reducers/setting.ts +++ b/src/settings/reducers/setting.ts @@ -1,18 +1,18 @@ import * as actions from '../actions'; import { - JSONSettings, FormSettings, SettingSource, + JSONTextSettings, FormSettings, SettingSource, } from '../../shared/SettingData'; export interface State { source: SettingSource; - json?: JSONSettings; + json?: JSONTextSettings; form?: FormSettings; error: string; } const defaultState: State = { source: SettingSource.JSON, - json: JSONSettings.valueOf(''), + json: JSONTextSettings.fromText(''), error: '', }; -- cgit v1.2.3 From 574692551a27ea56660bf2061daeaa0d34beaff4 Mon Sep 17 00:00:00 2001 From: Shin'ya UEOKA Date: Sat, 5 Oct 2019 02:06:02 +0000 Subject: Make Properties class --- src/background/repositories/SettingRepository.ts | 4 +- src/background/usecases/CompletionsUseCase.ts | 4 +- src/background/usecases/parsers.ts | 4 +- src/settings/components/form/PropertiesForm.tsx | 6 +- src/settings/components/index.tsx | 13 +-- src/shared/SettingData.ts | 15 +-- src/shared/Settings.ts | 37 +------ src/shared/properties.ts | 50 ---------- src/shared/property-defs.ts | 56 ----------- src/shared/settings/Properties.ts | 110 +++++++++++++++++++++ .../components/form/PropertiesForm.test.tsx | 4 +- test/shared/SettingData.test.ts | 15 +-- test/shared/Settings.test.ts | 31 +----- test/shared/properties.test.js | 18 ---- test/shared/property-defs.test.js | 18 ---- test/shared/settings/Properties.test.ts | 30 ++++++ 16 files changed, 179 insertions(+), 236 deletions(-) delete mode 100644 src/shared/properties.ts delete mode 100644 src/shared/property-defs.ts create mode 100644 src/shared/settings/Properties.ts delete mode 100644 test/shared/properties.test.js delete mode 100644 test/shared/property-defs.test.js create mode 100644 test/shared/settings/Properties.test.ts (limited to 'src/settings') diff --git a/src/background/repositories/SettingRepository.ts b/src/background/repositories/SettingRepository.ts index c7a0e84..a11b65f 100644 --- a/src/background/repositories/SettingRepository.ts +++ b/src/background/repositories/SettingRepository.ts @@ -1,7 +1,7 @@ import { injectable } from 'tsyringe'; import MemoryStorage from '../infrastructures/MemoryStorage'; import Settings, { valueOf, toJSON } from '../../shared/Settings'; -import * as PropertyDefs from '../../shared/property-defs'; +import Properties from '../../shared/settings/Properties'; const CACHED_SETTING_KEY = 'setting'; @@ -26,7 +26,7 @@ export default class SettingRepository { async setProperty( name: string, value: string | number | boolean, ): Promise { - let def = PropertyDefs.defs.find(d => name === d.name); + let def = Properties.def(name); if (!def) { throw new Error('unknown property: ' + name); } diff --git a/src/background/usecases/CompletionsUseCase.ts b/src/background/usecases/CompletionsUseCase.ts index 8cd4f32..bfff1e6 100644 --- a/src/background/usecases/CompletionsUseCase.ts +++ b/src/background/usecases/CompletionsUseCase.ts @@ -5,7 +5,7 @@ import CompletionsRepository from '../repositories/CompletionsRepository'; import * as filters from './filters'; import SettingRepository from '../repositories/SettingRepository'; import TabPresenter from '../presenters/TabPresenter'; -import * as PropertyDefs from '../../shared/property-defs'; +import Properties from '../../shared/settings/Properties'; const COMPLETION_ITEM_LIMIT = 10; @@ -129,7 +129,7 @@ export default class CompletionsUseCase { } querySet(name: string, keywords: string): Promise { - let items = PropertyDefs.defs.map((def) => { + let items = Properties.defs().map((def) => { if (def.type === 'boolean') { return [ { diff --git a/src/background/usecases/parsers.ts b/src/background/usecases/parsers.ts index 6135fd8..e8a1149 100644 --- a/src/background/usecases/parsers.ts +++ b/src/background/usecases/parsers.ts @@ -1,4 +1,4 @@ -import * as PropertyDefs from '../../shared//property-defs'; +import Properties from '../../shared/settings/Properties'; const mustNumber = (v: any): number => { let num = Number(v); @@ -16,7 +16,7 @@ const parseSetOption = ( value = !key.startsWith('no'); key = value ? key : key.slice(2); } - let def = PropertyDefs.defs.find(d => d.name === key); + let def = Properties.def(key); if (!def) { throw new Error('Unknown property: ' + key); } diff --git a/src/settings/components/form/PropertiesForm.tsx b/src/settings/components/form/PropertiesForm.tsx index ee98b7e..db8c8e5 100644 --- a/src/settings/components/form/PropertiesForm.tsx +++ b/src/settings/components/form/PropertiesForm.tsx @@ -18,7 +18,7 @@ class PropertiesForm extends React.Component { render() { let types = this.props.types; - let value = this.props.value; + let values = this.props.value; return
{ @@ -46,10 +46,10 @@ class PropertiesForm extends React.Component { {name}
; diff --git a/src/settings/components/index.tsx b/src/settings/components/index.tsx index ada6efb..e86d765 100644 --- a/src/settings/components/index.tsx +++ b/src/settings/components/index.tsx @@ -8,11 +8,11 @@ import BlacklistForm from './form/BlacklistForm'; import PropertiesForm from './form/PropertiesForm'; import * as settingActions from '../../settings/actions/setting'; import SettingData, { - JSONTextSettings, FormKeymaps, FormSearch, FormSettings, + FormKeymaps, FormSearch, FormSettings, JSONTextSettings, } from '../../shared/SettingData'; import { State as AppState } from '../reducers/setting'; import * as settings from '../../shared/Settings'; -import * as PropertyDefs from '../../shared/property-defs'; +import Properties from '../../shared/settings/Properties'; const DO_YOU_WANT_TO_CONTINUE = 'Some settings in JSON can be lost when migrating. ' + @@ -33,11 +33,6 @@ class SettingsComponent extends React.Component { } renderFormFields(form: any) { - let types = PropertyDefs.defs.reduce( - (o: {[key: string]: string}, def) => { - o[def.name] = def.type; - return o; - }, {}); return
Keybindings @@ -66,7 +61,7 @@ class SettingsComponent extends React.Component {
Properties { let data = new SettingData({ source: this.props.source, form: (this.props.form as FormSettings).buildWithProperties( - settings.propertiesValueOf(value)), + Properties.fromJSON(value)) }); this.props.dispatch(settingActions.set(data)); } diff --git a/src/shared/SettingData.ts b/src/shared/SettingData.ts index 6605c80..aa4e382 100644 --- a/src/shared/SettingData.ts +++ b/src/shared/SettingData.ts @@ -2,6 +2,7 @@ import * as operations from './operations'; import Settings, * as settings from './Settings'; import Keymaps from './settings/Keymaps'; import Search from './settings/Search'; +import Properties from './settings/Properties'; export class FormKeymaps { private data: {[op: string]: string}; @@ -143,14 +144,14 @@ export class FormSettings { private search: FormSearch; - private properties: settings.Properties; + private properties: Properties; private blacklist: string[]; constructor( keymaps: FormKeymaps, search: FormSearch, - properties: settings.Properties, + properties: Properties, blacklist: string[], ) { this.keymaps = keymaps; @@ -177,7 +178,7 @@ export class FormSettings { ); } - buildWithProperties(props: settings.Properties): FormSettings { + buildWithProperties(props: Properties): FormSettings { return new FormSettings( this.keymaps, this.search, @@ -199,7 +200,7 @@ export class FormSettings { return settings.valueOf({ keymaps: this.keymaps.toKeymaps().toJSON(), search: this.search.toSearchSettings().toJSON(), - properties: this.properties, + properties: this.properties.toJSON(), blacklist: this.blacklist, }); } @@ -207,13 +208,13 @@ export class FormSettings { toJSON(): { keymaps: ReturnType; search: ReturnType; - properties: settings.Properties; + properties: ReturnType; blacklist: string[]; } { return { keymaps: this.keymaps.toJSON(), search: this.search.toJSON(), - properties: this.properties, + properties: this.properties.toJSON(), blacklist: this.blacklist, }; } @@ -227,7 +228,7 @@ export class FormSettings { return new FormSettings( FormKeymaps.valueOf(o.keymaps), FormSearch.valueOf(o.search), - settings.propertiesValueOf(o.properties), + Properties.fromJSON(o.properties), settings.blacklistValueOf(o.blacklist), ); } diff --git a/src/shared/Settings.ts b/src/shared/Settings.ts index e2bb3f4..6250aae 100644 --- a/src/shared/Settings.ts +++ b/src/shared/Settings.ts @@ -1,12 +1,6 @@ -import * as PropertyDefs from './property-defs'; import Keymaps from './settings/Keymaps'; import Search from './settings/Search'; - -export interface Properties { - hintchars: string; - smoothscroll: boolean; - complete: string; -} +import Properties from './settings/Properties'; export default interface Settings { keymaps: Keymaps; @@ -15,27 +9,6 @@ export default interface Settings { blacklist: string[]; } -export const propertiesValueOf = (o: any): Properties => { - let defNames = new Set(PropertyDefs.defs.map(def => def.name)); - let unknownName = Object.keys(o).find(name => !defNames.has(name)); - if (unknownName) { - throw new TypeError(`Unknown property name: "${unknownName}"`); - } - - for (let def of PropertyDefs.defs) { - if (!Object.prototype.hasOwnProperty.call(o, def.name)) { - continue; - } - if (typeof o[def.name] !== def.type) { - throw new TypeError(`property "${def.name}" is not ${def.type}`); - } - } - return { - ...PropertyDefs.defaultValues, - ...o, - }; -}; - export const blacklistValueOf = (o: any): string[] => { if (!Array.isArray(o)) { throw new TypeError(`"blacklist" is not an array of string`); @@ -59,7 +32,7 @@ export const valueOf = (o: any): Settings => { settings.search = Search.fromJSON(o.search); break; case 'properties': - settings.properties = propertiesValueOf(o.properties); + settings.properties = Properties.fromJSON(o.properties); break; case 'blacklist': settings.blacklist = blacklistValueOf(o.blacklist); @@ -75,7 +48,7 @@ export const toJSON = (settings: Settings): any => { return { keymaps: settings.keymaps.toJSON(), search: settings.search.toJSON(), - properties: settings.properties, + properties: settings.properties.toJSON(), blacklist: settings.blacklist, }; }; @@ -156,10 +129,10 @@ export const DefaultSetting: Settings = { 'wikipedia': 'https://en.wikipedia.org/w/index.php?search={}' } }), - properties: { + properties: Properties.fromJSON({ hintchars: 'abcdefghijklmnopqrstuvwxyz', smoothscroll: false, complete: 'sbh' - }, + }), blacklist: [] }; diff --git a/src/shared/properties.ts b/src/shared/properties.ts deleted file mode 100644 index 6315030..0000000 --- a/src/shared/properties.ts +++ /dev/null @@ -1,50 +0,0 @@ -export type Type = string | number | boolean; - -export class Def { - private name0: string; - - private description0: string; - - private defaultValue0: Type; - - constructor( - name: string, - description: string, - defaultValue: Type, - ) { - this.name0 = name; - this.description0 = description; - this.defaultValue0 = defaultValue; - } - - public get name(): string { - return this.name0; - } - - public get defaultValue(): Type { - return this.defaultValue0; - } - - public get description(): Type { - return this.description0; - } - - public get type(): string { - return typeof this.defaultValue; - } -} - -export const defs: Def[] = [ - new Def( - 'hintchars', - 'hint characters on follow mode', - 'abcdefghijklmnopqrstuvwxyz'), - new Def( - 'smoothscroll', - 'smooth scroll', - false), - new Def( - 'complete', - 'which are completed at the open page', - 'sbh'), -]; diff --git a/src/shared/property-defs.ts b/src/shared/property-defs.ts deleted file mode 100644 index fec9f80..0000000 --- a/src/shared/property-defs.ts +++ /dev/null @@ -1,56 +0,0 @@ -export type Type = string | number | boolean; - -export class Def { - private name0: string; - - private description0: string; - - private defaultValue0: Type; - - constructor( - name: string, - description: string, - defaultValue: Type, - ) { - this.name0 = name; - this.description0 = description; - this.defaultValue0 = defaultValue; - } - - public get name(): string { - return this.name0; - } - - public get defaultValue(): Type { - return this.defaultValue0; - } - - public get description(): Type { - return this.description0; - } - - public get type(): string { - return typeof this.defaultValue; - } -} - -export const defs: Def[] = [ - new Def( - 'hintchars', - 'hint characters on follow mode', - 'abcdefghijklmnopqrstuvwxyz'), - new Def( - 'smoothscroll', - 'smooth scroll', - false), - new Def( - 'complete', - 'which are completed at the open page', - 'sbh'), -]; - -export const defaultValues = { - hintchars: 'abcdefghijklmnopqrstuvwxyz', - smoothscroll: false, - complete: 'sbh', -}; diff --git a/src/shared/settings/Properties.ts b/src/shared/settings/Properties.ts new file mode 100644 index 0000000..1bc4c7f --- /dev/null +++ b/src/shared/settings/Properties.ts @@ -0,0 +1,110 @@ +export type PropertiesJSON = { + hintchars?: string; + smoothscroll?: boolean; + complete?: string; +}; + +export type PropertyTypes = { + hintchars: string; + smoothscroll: string; + complete: string; +}; + +type PropertyName = 'hintchars' | 'smoothscroll' | 'complete'; + +type PropertyDef = { + name: PropertyName; + description: string; + defaultValue: string | number | boolean; + type: 'string' | 'number' | 'boolean'; +}; + +const defs: PropertyDef[] = [ + { + name: 'hintchars', + description: 'hint characters on follow mode', + defaultValue: 'abcdefghijklmnopqrstuvwxyz', + type: 'string', + }, { + name: 'smoothscroll', + description: 'smooth scroll', + defaultValue: false, + type: 'boolean', + }, { + name: 'complete', + description: 'which are completed at the open page', + defaultValue: 'sbh', + type: 'string', + } +]; + +const defaultValues = { + hintchars: 'abcdefghijklmnopqrstuvwxyz', + smoothscroll: false, + complete: 'sbh', +}; + +export default class Properties { + public hintchars: string; + + public smoothscroll: boolean; + + public complete: string; + + constructor({ + hintchars, + smoothscroll, + complete, + }: { + hintchars?: string; + smoothscroll?: boolean; + complete?: string; + }) { + this.hintchars = hintchars || defaultValues.hintchars; + this.smoothscroll = smoothscroll || defaultValues.smoothscroll; + this.complete = complete || defaultValues.complete; + } + + static fromJSON(json: any): Properties { + let defNames: Set = new Set(defs.map(def => def.name)); + let unknownName = Object.keys(json).find(name => !defNames.has(name)); + if (unknownName) { + throw new TypeError(`Unknown property name: "${unknownName}"`); + } + + for (let def of defs) { + if (!Object.prototype.hasOwnProperty.call(json, def.name)) { + continue; + } + if (typeof json[def.name] !== def.type) { + throw new TypeError( + `property "${def.name}" is not ${def.type}`); + } + } + return new Properties(json); + } + + static types(): PropertyTypes { + return { + hintchars: 'string', + smoothscroll: 'boolean', + complete: 'string', + }; + } + + static def(name: string): PropertyDef | undefined { + return defs.find(p => p.name === name); + } + + static defs(): PropertyDef[] { + return defs; + } + + toJSON(): PropertiesJSON { + return { + hintchars: this.hintchars, + smoothscroll: this.smoothscroll, + complete: this.complete, + }; + } +} diff --git a/test/settings/components/form/PropertiesForm.test.tsx b/test/settings/components/form/PropertiesForm.test.tsx index 80f60d2..0e33cc8 100644 --- a/test/settings/components/form/PropertiesForm.test.tsx +++ b/test/settings/components/form/PropertiesForm.test.tsx @@ -13,14 +13,14 @@ describe("settings/form/PropertiesForm", () => { mybool: 'boolean', empty: 'string', } - let value = { + let values = { mystr: 'abc', mynum: 123, mybool: true, }; let root = ReactTestRenderer.create( - , + , ).root let input = root.findByProps({ name: 'mystr' }); diff --git a/test/shared/SettingData.test.ts b/test/shared/SettingData.test.ts index f8995d9..b5bf70e 100644 --- a/test/shared/SettingData.test.ts +++ b/test/shared/SettingData.test.ts @@ -5,6 +5,7 @@ import Settings from '../../src/shared/Settings'; import { expect } from 'chai'; import Keymaps from '../../src/shared/settings/Keymaps'; import Search from '../../src/shared/settings/Search'; +import Properties from '../../src/shared/settings/Properties'; describe('shared/SettingData', () => { describe('FormKeymaps', () => { @@ -62,7 +63,7 @@ describe('shared/SettingData', () => { expect({ keymaps: settings.keymaps.toJSON(), search: settings.search.toJSON(), - properties: settings.properties, + properties: settings.properties.toJSON(), blacklist: settings.blacklist, }).to.deep.equal(JSON.parse(o)); }); @@ -78,11 +79,11 @@ describe('shared/SettingData', () => { google: "https://google.com/search?q={}", }, }), - properties: { + properties: Properties.fromJSON({ hintchars: "abcdefghijklmnopqrstuvwxyz", smoothscroll: false, complete: "sbh" - }, + }), blacklist: [], }; @@ -90,7 +91,7 @@ describe('shared/SettingData', () => { expect(JSON.parse(json)).to.deep.equal({ keymaps: o.keymaps.toJSON(), search: o.search.toJSON(), - properties: o.properties, + properties: o.properties.toJSON(), blacklist: o.blacklist, }); }); @@ -123,7 +124,7 @@ describe('shared/SettingData', () => { expect({ keymaps: settings.keymaps.toJSON(), search: settings.search.toJSON(), - properties: settings.properties, + properties: settings.properties.toJSON(), blacklist: settings.blacklist, }).to.deep.equal({ keymaps: { @@ -159,11 +160,11 @@ describe('shared/SettingData', () => { "google": "https://google.com/search?q={}" } }), - properties: { + properties: Properties.fromJSON({ hintchars: "abcdefghijklmnopqrstuvwxyz", smoothscroll: false, complete: "sbh" - }, + }), blacklist: [] }; diff --git a/test/shared/Settings.test.ts b/test/shared/Settings.test.ts index ed791a1..6360bab 100644 --- a/test/shared/Settings.test.ts +++ b/test/shared/Settings.test.ts @@ -1,33 +1,8 @@ import * as settings from '../../src/shared/Settings'; -import { expect } from 'chai'; +import {expect} from 'chai'; describe('Settings', () => { - describe('#propertiesValueOf', () => { - it('returns with default properties by empty settings', () => { - let props = settings.propertiesValueOf({}); - expect(props).to.deep.equal({ - hintchars: "abcdefghijklmnopqrstuvwxyz", - smoothscroll: false, - complete: "sbh" - }) - }); - - it('returns properties by valid settings', () => { - let props = settings.propertiesValueOf({ - hintchars: "abcdefgh", - smoothscroll: false, - complete: "sbh" - }); - - expect(props).to.deep.equal({ - hintchars: "abcdefgh", - smoothscroll: false, - complete: "sbh" - }); - }); - }); - describe('#blacklistValueOf', () => { it('returns empty array by empty settings', () => { let blacklist = settings.blacklistValueOf([]); @@ -70,7 +45,7 @@ describe('Settings', () => { expect({ keymaps: x.keymaps.toJSON(), search: x.search.toJSON(), - properties: x.properties, + properties: x.properties.toJSON(), blacklist: x.blacklist, }).to.deep.equal({ keymaps: {}, @@ -92,7 +67,7 @@ describe('Settings', () => { it('sets default settings', () => { let value = settings.valueOf({}); expect(value.keymaps.toJSON()).to.not.be.empty; - expect(value.properties).to.not.be.empty; + expect(value.properties.toJSON()).to.not.be.empty; expect(value.search.defaultEngine).to.be.a('string'); expect(value.search.engines).to.be.an('object'); expect(value.blacklist).to.be.empty; diff --git a/test/shared/properties.test.js b/test/shared/properties.test.js deleted file mode 100644 index 37903d8..0000000 --- a/test/shared/properties.test.js +++ /dev/null @@ -1,18 +0,0 @@ -import * as settings from 'shared/settings'; - -describe('properties', () => { - describe('Def class', () => { - it('returns property definitions', () => { - let def = new proerties.Def( - 'smoothscroll', - 'smooth scroll', - false); - - expect(def.name).to.equal('smoothscroll'); - expect(def.describe).to.equal('smooth scroll'); - expect(def.defaultValue).to.equal(false); - expect(def.type).to.equal('boolean'); - }); - }); -}); - diff --git a/test/shared/property-defs.test.js b/test/shared/property-defs.test.js deleted file mode 100644 index 37903d8..0000000 --- a/test/shared/property-defs.test.js +++ /dev/null @@ -1,18 +0,0 @@ -import * as settings from 'shared/settings'; - -describe('properties', () => { - describe('Def class', () => { - it('returns property definitions', () => { - let def = new proerties.Def( - 'smoothscroll', - 'smooth scroll', - false); - - expect(def.name).to.equal('smoothscroll'); - expect(def.describe).to.equal('smooth scroll'); - expect(def.defaultValue).to.equal(false); - expect(def.type).to.equal('boolean'); - }); - }); -}); - diff --git a/test/shared/settings/Properties.test.ts b/test/shared/settings/Properties.test.ts new file mode 100644 index 0000000..609a565 --- /dev/null +++ b/test/shared/settings/Properties.test.ts @@ -0,0 +1,30 @@ +import Properties from '../../../src/shared/settings/Properties'; +import { expect } from 'chai'; + +describe('Properties', () => { + describe('#propertiesValueOf', () => { + it('returns with default properties by empty settings', () => { + let props = Properties.fromJSON({}); + expect(props).to.deep.equal({ + hintchars: "abcdefghijklmnopqrstuvwxyz", + smoothscroll: false, + complete: "sbh" + }) + }); + + it('returns properties by valid settings', () => { + let props = Properties.fromJSON({ + hintchars: "abcdefgh", + smoothscroll: false, + complete: "sbh" + }); + + expect(props).to.deep.equal({ + hintchars: "abcdefgh", + smoothscroll: false, + complete: "sbh" + }); + }); + }); +}); + -- cgit v1.2.3 From b86b4680b6c2a3f0cceefaaf7c2e35417ec555df Mon Sep 17 00:00:00 2001 From: Shin'ya UEOKA Date: Sat, 5 Oct 2019 06:39:20 +0000 Subject: Make Blacklist class --- src/content/controllers/SettingController.ts | 6 +-- src/settings/components/index.tsx | 4 +- src/shared/SettingData.ts | 13 ++--- src/shared/Settings.ts | 21 ++------ src/shared/blacklists.ts | 13 ----- src/shared/settings/Blacklist.ts | 39 ++++++++++++++ src/shared/utils/re.ts | 6 --- test/shared/SettingData.test.ts | 11 ++-- test/shared/Settings.test.ts | 30 +---------- test/shared/blacklists.test.ts | 49 ------------------ test/shared/settings/Blacklist.test.ts | 77 ++++++++++++++++++++++++++++ test/shared/utils/re.test.ts | 19 ------- 12 files changed, 139 insertions(+), 149 deletions(-) delete mode 100644 src/shared/blacklists.ts create mode 100644 src/shared/settings/Blacklist.ts delete mode 100644 src/shared/utils/re.ts delete mode 100644 test/shared/blacklists.test.ts create mode 100644 test/shared/settings/Blacklist.test.ts delete mode 100644 test/shared/utils/re.test.ts (limited to 'src/settings') diff --git a/src/content/controllers/SettingController.ts b/src/content/controllers/SettingController.ts index 7fb045b..06273a0 100644 --- a/src/content/controllers/SettingController.ts +++ b/src/content/controllers/SettingController.ts @@ -1,8 +1,6 @@ import { injectable } from 'tsyringe'; import AddonEnabledUseCase from '../usecases/AddonEnabledUseCase'; import SettingUseCase from '../usecases/SettingUseCase'; -import * as blacklists from '../../shared/blacklists'; - import * as messages from '../../shared/messages'; @injectable() @@ -17,9 +15,7 @@ export default class SettingController { async initSettings(): Promise { try { let current = await this.settingUseCase.reload(); - let disabled = blacklists.includes( - current.blacklist, window.location.href, - ); + let disabled = current.blacklist.includes(window.location.href); if (disabled) { this.addonEnabledUseCase.disable(); } else { diff --git a/src/settings/components/index.tsx b/src/settings/components/index.tsx index e86d765..bd99d52 100644 --- a/src/settings/components/index.tsx +++ b/src/settings/components/index.tsx @@ -11,8 +11,8 @@ import SettingData, { FormKeymaps, FormSearch, FormSettings, JSONTextSettings, } from '../../shared/SettingData'; import { State as AppState } from '../reducers/setting'; -import * as settings from '../../shared/Settings'; import Properties from '../../shared/settings/Properties'; +import Blacklist from '../../shared/settings/Blacklist'; const DO_YOU_WANT_TO_CONTINUE = 'Some settings in JSON can be lost when migrating. ' + @@ -143,7 +143,7 @@ class SettingsComponent extends React.Component { let data = new SettingData({ source: this.props.source, form: (this.props.form as FormSettings).buildWithBlacklist( - settings.blacklistValueOf(value)), + Blacklist.fromJSON(value)), }); this.props.dispatch(settingActions.set(data)); } diff --git a/src/shared/SettingData.ts b/src/shared/SettingData.ts index aa4e382..8ef8385 100644 --- a/src/shared/SettingData.ts +++ b/src/shared/SettingData.ts @@ -3,6 +3,7 @@ import Settings, * as settings from './Settings'; import Keymaps from './settings/Keymaps'; import Search from './settings/Search'; import Properties from './settings/Properties'; +import Blacklist from './settings/Blacklist'; export class FormKeymaps { private data: {[op: string]: string}; @@ -146,13 +147,13 @@ export class FormSettings { private properties: Properties; - private blacklist: string[]; + private blacklist: Blacklist; constructor( keymaps: FormKeymaps, search: FormSearch, properties: Properties, - blacklist: string[], + blacklist: Blacklist, ) { this.keymaps = keymaps; this.search = search; @@ -187,7 +188,7 @@ export class FormSettings { ); } - buildWithBlacklist(blacklist: string[]): FormSettings { + buildWithBlacklist(blacklist: Blacklist): FormSettings { return new FormSettings( this.keymaps, this.search, @@ -201,7 +202,7 @@ export class FormSettings { keymaps: this.keymaps.toKeymaps().toJSON(), search: this.search.toSearchSettings().toJSON(), properties: this.properties.toJSON(), - blacklist: this.blacklist, + blacklist: this.blacklist.toJSON(), }); } @@ -215,7 +216,7 @@ export class FormSettings { keymaps: this.keymaps.toJSON(), search: this.search.toJSON(), properties: this.properties.toJSON(), - blacklist: this.blacklist, + blacklist: this.blacklist.toJSON(), }; } @@ -229,7 +230,7 @@ export class FormSettings { FormKeymaps.valueOf(o.keymaps), FormSearch.valueOf(o.search), Properties.fromJSON(o.properties), - settings.blacklistValueOf(o.blacklist), + Blacklist.fromJSON(o.blacklist), ); } diff --git a/src/shared/Settings.ts b/src/shared/Settings.ts index 6250aae..2767820 100644 --- a/src/shared/Settings.ts +++ b/src/shared/Settings.ts @@ -1,26 +1,15 @@ import Keymaps from './settings/Keymaps'; import Search from './settings/Search'; import Properties from './settings/Properties'; +import Blacklist from './settings/Blacklist'; export default interface Settings { keymaps: Keymaps; search: Search; properties: Properties; - blacklist: string[]; + blacklist: Blacklist; } -export const blacklistValueOf = (o: any): string[] => { - if (!Array.isArray(o)) { - throw new TypeError(`"blacklist" is not an array of string`); - } - for (let x of o) { - if (typeof x !== 'string') { - throw new TypeError(`"blacklist" is not an array of string`); - } - } - return o as string[]; -}; - export const valueOf = (o: any): Settings => { let settings = { ...DefaultSetting }; for (let key of Object.keys(o)) { @@ -35,7 +24,7 @@ export const valueOf = (o: any): Settings => { settings.properties = Properties.fromJSON(o.properties); break; case 'blacklist': - settings.blacklist = blacklistValueOf(o.blacklist); + settings.blacklist = Blacklist.fromJSON(o.blacklist); break; default: throw new TypeError('unknown setting: ' + key); @@ -49,7 +38,7 @@ export const toJSON = (settings: Settings): any => { keymaps: settings.keymaps.toJSON(), search: settings.search.toJSON(), properties: settings.properties.toJSON(), - blacklist: settings.blacklist, + blacklist: settings.blacklist.toJSON(), }; }; @@ -134,5 +123,5 @@ export const DefaultSetting: Settings = { smoothscroll: false, complete: 'sbh' }), - blacklist: [] + blacklist: Blacklist.fromJSON([]), }; diff --git a/src/shared/blacklists.ts b/src/shared/blacklists.ts deleted file mode 100644 index 61ee4de..0000000 --- a/src/shared/blacklists.ts +++ /dev/null @@ -1,13 +0,0 @@ -import * as re from './utils/re'; - -const includes = (blacklist: string[], url: string): boolean => { - let u = new URL(url); - return blacklist.some((item) => { - if (!item.includes('/')) { - return re.fromWildcard(item).test(u.host); - } - return re.fromWildcard(item).test(u.host + u.pathname); - }); -}; - -export { includes }; diff --git a/src/shared/settings/Blacklist.ts b/src/shared/settings/Blacklist.ts new file mode 100644 index 0000000..a95b606 --- /dev/null +++ b/src/shared/settings/Blacklist.ts @@ -0,0 +1,39 @@ +export type BlacklistJSON = string[]; + +const fromWildcard = (pattern: string): RegExp => { + let regexStr = '^' + pattern.replace(/\*/g, '.*') + '$'; + return new RegExp(regexStr); +}; + +export default class Blacklist { + constructor( + private blacklist: string[], + ) { + } + + static fromJSON(json: any): Blacklist { + if (!Array.isArray(json)) { + throw new TypeError(`"blacklist" is not an array of string`); + } + for (let x of json) { + if (typeof x !== 'string') { + throw new TypeError(`"blacklist" is not an array of string`); + } + } + return new Blacklist(json); + } + + toJSON(): BlacklistJSON { + return this.blacklist; + } + + includes(url: string): boolean { + let u = new URL(url); + return this.blacklist.some((item) => { + if (!item.includes('/')) { + return fromWildcard(item).test(u.host); + } + return fromWildcard(item).test(u.host + u.pathname); + }); + } +} diff --git a/src/shared/utils/re.ts b/src/shared/utils/re.ts deleted file mode 100644 index 34f4fa6..0000000 --- a/src/shared/utils/re.ts +++ /dev/null @@ -1,6 +0,0 @@ -const fromWildcard = (pattern: string): RegExp => { - let regexStr = '^' + pattern.replace(/\*/g, '.*') + '$'; - return new RegExp(regexStr); -}; - -export { fromWildcard }; diff --git a/test/shared/SettingData.test.ts b/test/shared/SettingData.test.ts index b5bf70e..4391e73 100644 --- a/test/shared/SettingData.test.ts +++ b/test/shared/SettingData.test.ts @@ -6,6 +6,7 @@ import { expect } from 'chai'; import Keymaps from '../../src/shared/settings/Keymaps'; import Search from '../../src/shared/settings/Search'; import Properties from '../../src/shared/settings/Properties'; +import Blacklist from '../../src/shared/settings/Blacklist' describe('shared/SettingData', () => { describe('FormKeymaps', () => { @@ -64,7 +65,7 @@ describe('shared/SettingData', () => { keymaps: settings.keymaps.toJSON(), search: settings.search.toJSON(), properties: settings.properties.toJSON(), - blacklist: settings.blacklist, + blacklist: settings.blacklist.toJSON(), }).to.deep.equal(JSON.parse(o)); }); }); @@ -84,7 +85,7 @@ describe('shared/SettingData', () => { smoothscroll: false, complete: "sbh" }), - blacklist: [], + blacklist: Blacklist.fromJSON([]), }; let json = JSONTextSettings.fromSettings(o).toJSONText(); @@ -92,7 +93,7 @@ describe('shared/SettingData', () => { keymaps: o.keymaps.toJSON(), search: o.search.toJSON(), properties: o.properties.toJSON(), - blacklist: o.blacklist, + blacklist: o.blacklist.toJSON(), }); }); }); @@ -125,7 +126,7 @@ describe('shared/SettingData', () => { keymaps: settings.keymaps.toJSON(), search: settings.search.toJSON(), properties: settings.properties.toJSON(), - blacklist: settings.blacklist, + blacklist: settings.blacklist.toJSON(), }).to.deep.equal({ keymaps: { 'j': { type: 'scroll.vertically', count: 1 }, @@ -165,7 +166,7 @@ describe('shared/SettingData', () => { smoothscroll: false, complete: "sbh" }), - blacklist: [] + blacklist: Blacklist.fromJSON([]), }; let json = FormSettings.fromSettings(data).toJSON(); diff --git a/test/shared/Settings.test.ts b/test/shared/Settings.test.ts index 6360bab..9688798 100644 --- a/test/shared/Settings.test.ts +++ b/test/shared/Settings.test.ts @@ -2,32 +2,6 @@ import * as settings from '../../src/shared/Settings'; import {expect} from 'chai'; describe('Settings', () => { - - describe('#blacklistValueOf', () => { - it('returns empty array by empty settings', () => { - let blacklist = settings.blacklistValueOf([]); - expect(blacklist).to.be.empty; - }); - - it('returns blacklist by valid settings', () => { - let blacklist = settings.blacklistValueOf([ - "github.com", - "circleci.com", - ]); - - expect(blacklist).to.deep.equal([ - "github.com", - "circleci.com", - ]); - }); - - it('throws a TypeError by invalid settings', () => { - expect(() => settings.blacklistValueOf(null)).to.throw(TypeError); - expect(() => settings.blacklistValueOf({})).to.throw(TypeError); - expect(() => settings.blacklistValueOf([1,2,3])).to.throw(TypeError); - }); - }); - describe('#valueOf', () => { it('returns settings by valid settings', () => { let x = settings.valueOf({ @@ -46,7 +20,7 @@ describe('Settings', () => { keymaps: x.keymaps.toJSON(), search: x.search.toJSON(), properties: x.properties.toJSON(), - blacklist: x.blacklist, + blacklist: x.blacklist.toJSON(), }).to.deep.equal({ keymaps: {}, search: { @@ -70,7 +44,7 @@ describe('Settings', () => { expect(value.properties.toJSON()).to.not.be.empty; expect(value.search.defaultEngine).to.be.a('string'); expect(value.search.engines).to.be.an('object'); - expect(value.blacklist).to.be.empty; + expect(value.blacklist.toJSON()).to.be.empty; }); it('throws a TypeError with an unknown field', () => { diff --git a/test/shared/blacklists.test.ts b/test/shared/blacklists.test.ts deleted file mode 100644 index 289ea0f..0000000 --- a/test/shared/blacklists.test.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { includes } from 'shared/blacklists'; - -describe("shared/blacklist", () => { - it('matches by *', () => { - let blacklist = ['*']; - - expect(includes(blacklist, 'https://github.com/abc')).to.be.true; - }) - - it('matches by hostname', () => { - let blacklist = ['github.com']; - - expect(includes(blacklist, 'https://github.com')).to.be.true; - expect(includes(blacklist, 'https://gist.github.com')).to.be.false; - expect(includes(blacklist, 'https://github.com/ueokande')).to.be.true; - expect(includes(blacklist, 'https://github.org')).to.be.false; - expect(includes(blacklist, 'https://google.com/search?q=github.org')).to.be.false; - }) - - it('matches by hostname with wildcard', () => { - let blacklist = ['*.github.com']; - - expect(includes(blacklist, 'https://github.com')).to.be.false; - expect(includes(blacklist, 'https://gist.github.com')).to.be.true; - }) - - it('matches by path', () => { - let blacklist = ['github.com/abc']; - - expect(includes(blacklist, 'https://github.com/abc')).to.be.true; - expect(includes(blacklist, 'https://github.com/abcdef')).to.be.false; - expect(includes(blacklist, 'https://gist.github.com/abc')).to.be.false; - }) - - it('matches by path with wildcard', () => { - let blacklist = ['github.com/abc*']; - - expect(includes(blacklist, 'https://github.com/abc')).to.be.true; - expect(includes(blacklist, 'https://github.com/abcdef')).to.be.true; - expect(includes(blacklist, 'https://gist.github.com/abc')).to.be.false; - }) - - it('matches address and port', () => { - let blacklist = ['127.0.0.1:8888']; - - expect(includes(blacklist, 'http://127.0.0.1:8888/')).to.be.true; - expect(includes(blacklist, 'http://127.0.0.1:8888/hello')).to.be.true; - }) -}); diff --git a/test/shared/settings/Blacklist.test.ts b/test/shared/settings/Blacklist.test.ts new file mode 100644 index 0000000..fbacf5d --- /dev/null +++ b/test/shared/settings/Blacklist.test.ts @@ -0,0 +1,77 @@ +import Blacklist from '../../../src/shared/settings/Blacklist'; +import { expect } from 'chai'; + +describe('Blacklist', () => { + describe('fromJSON', () => { + it('returns empty array by empty settings', () => { + let blacklist = Blacklist.fromJSON([]); + expect(blacklist.toJSON()).to.be.empty; + }); + + it('returns blacklist by valid settings', () => { + let blacklist = Blacklist.fromJSON([ + 'github.com', + 'circleci.com', + ]); + + expect(blacklist.toJSON()).to.deep.equal([ + 'github.com', + 'circleci.com', + ]); + }); + + it('throws a TypeError by invalid settings', () => { + expect(() => Blacklist.fromJSON(null)).to.throw(TypeError); + expect(() => Blacklist.fromJSON({})).to.throw(TypeError); + expect(() => Blacklist.fromJSON([1,2,3])).to.throw(TypeError); + }); + }); + + describe('#includes', () => { + it('matches by *', () => { + let blacklist = new Blacklist(['*']); + + expect(blacklist.includes('https://github.com/abc')).to.be.true; + }); + + it('matches by hostname', () => { + let blacklist = new Blacklist(['github.com']); + + expect(blacklist.includes('https://github.com')).to.be.true; + expect(blacklist.includes('https://gist.github.com')).to.be.false; + expect(blacklist.includes('https://github.com/ueokande')).to.be.true; + expect(blacklist.includes('https://github.org')).to.be.false; + expect(blacklist.includes('https://google.com/search?q=github.org')).to.be.false; + }); + + it('matches by hostname with wildcard', () => { + let blacklist = new Blacklist(['*.github.com']); + + expect(blacklist.includes('https://github.com')).to.be.false; + expect(blacklist.includes('https://gist.github.com')).to.be.true; + }) + + it('matches by path', () => { + let blacklist = new Blacklist(['github.com/abc']); + + expect(blacklist.includes('https://github.com/abc')).to.be.true; + expect(blacklist.includes('https://github.com/abcdef')).to.be.false; + expect(blacklist.includes('https://gist.github.com/abc')).to.be.false; + }) + + it('matches by path with wildcard', () => { + let blacklist = new Blacklist(['github.com/abc*']); + + expect(blacklist.includes('https://github.com/abc')).to.be.true; + expect(blacklist.includes('https://github.com/abcdef')).to.be.true; + expect(blacklist.includes('https://gist.github.com/abc')).to.be.false; + }) + + it('matches address and port', () => { + let blacklist = new Blacklist(['127.0.0.1:8888']); + + expect(blacklist.includes('http://127.0.0.1:8888/')).to.be.true; + expect(blacklist.includes('http://127.0.0.1:8888/hello')).to.be.true; + }) + }) +}); diff --git a/test/shared/utils/re.test.ts b/test/shared/utils/re.test.ts deleted file mode 100644 index d12ceb7..0000000 --- a/test/shared/utils/re.test.ts +++ /dev/null @@ -1,19 +0,0 @@ -import * as re from 'shared/utils/re'; - -describe("re util", () => { - it('matches by pattern', () => { - let regex = re.fromWildcard('*.example.com/*'); - expect('foo.example.com/bar').to.match(regex); - expect('foo.example.com').not.to.match(regex); - expect('example.com/bar').not.to.match(regex); - - regex = re.fromWildcard('example.com/*') - expect('example.com/foo').to.match(regex); - expect('example.com/').to.match(regex); - - regex = re.fromWildcard('example.com/*bar') - expect('example.com/foobar').to.match(regex); - expect('example.com/bar').to.match(regex); - expect('example.com/foobarfoo').not.to.match(regex); - }) -}); -- cgit v1.2.3 From 8428671a0acf47a4a90b6b1f39cf94401f1e5520 Mon Sep 17 00:00:00 2001 From: Shin'ya UEOKA Date: Sat, 5 Oct 2019 13:56:21 +0000 Subject: Fix form options --- src/settings/components/form/BlacklistForm.tsx | 38 +++++++++++++++----------- src/settings/components/index.tsx | 11 ++++---- src/settings/reducers/setting.ts | 2 ++ src/shared/SettingData.ts | 10 +++---- src/shared/settings/Properties.ts | 2 +- 5 files changed, 35 insertions(+), 28 deletions(-) (limited to 'src/settings') diff --git a/src/settings/components/form/BlacklistForm.tsx b/src/settings/components/form/BlacklistForm.tsx index 637bc1e..f352e41 100644 --- a/src/settings/components/form/BlacklistForm.tsx +++ b/src/settings/components/form/BlacklistForm.tsx @@ -2,10 +2,11 @@ import './BlacklistForm.scss'; import AddButton from '../ui/AddButton'; import DeleteButton from '../ui/DeleteButton'; import React from 'react'; +import { BlacklistJSON } from '../../../shared/settings/Blacklist'; interface Props { - value: string[]; - onChange: (value: string[]) => void; + value: BlacklistJSON; + onChange: (value: BlacklistJSON) => void; onBlur: () => void; } @@ -19,19 +20,24 @@ class BlacklistForm extends React.Component { render() { return
{ - this.props.value.map((url, index) => { - return
- - -
; - }) + this.props.value + .map((item, index) => { + if (typeof item !== 'string') { + // TODO support partial blacklist; + return null; + } + return
+ + +
; + }) } @@ -41,7 +47,7 @@ class BlacklistForm extends React.Component { bindValue(e: any) { let name = e.target.name; let index = e.target.getAttribute('data-index'); - let next = this.props.value ? this.props.value.slice() : []; + let next = this.props.value.slice(); if (name === 'url') { next[index] = e.target.value; diff --git a/src/settings/components/index.tsx b/src/settings/components/index.tsx index bd99d52..586f0f0 100644 --- a/src/settings/components/index.tsx +++ b/src/settings/components/index.tsx @@ -32,7 +32,7 @@ class SettingsComponent extends React.Component { this.props.dispatch(settingActions.load()); } - renderFormFields(form: any) { + renderFormFields(form: FormSettings) { return
Keybindings @@ -53,7 +53,7 @@ class SettingsComponent extends React.Component {
Blacklist @@ -62,7 +62,7 @@ class SettingsComponent extends React.Component { Properties @@ -89,10 +89,9 @@ class SettingsComponent extends React.Component { let fields = null; let disabled = this.props.error.length > 0; if (this.props.source === 'form') { - fields = this.renderFormFields(this.props.form); + fields = this.renderFormFields(this.props.form!!); } else if (this.props.source === 'json') { - fields = this.renderJsonFields( - this.props.json as JSONTextSettings, this.props.error); + fields = this.renderJsonFields(this.props.json!!, this.props.error); } return (
diff --git a/src/settings/reducers/setting.ts b/src/settings/reducers/setting.ts index 89bf1cb..804853f 100644 --- a/src/settings/reducers/setting.ts +++ b/src/settings/reducers/setting.ts @@ -2,6 +2,7 @@ import * as actions from '../actions'; import { JSONTextSettings, FormSettings, SettingSource, } from '../../shared/SettingData'; +import { DefaultSetting } from '../../shared/settings/Settings'; export interface State { source: SettingSource; @@ -13,6 +14,7 @@ export interface State { const defaultState: State = { source: SettingSource.JSON, json: JSONTextSettings.fromText(''), + form: FormSettings.fromSettings(DefaultSetting), error: '', }; diff --git a/src/shared/SettingData.ts b/src/shared/SettingData.ts index 2dedfef..c28a5dd 100644 --- a/src/shared/SettingData.ts +++ b/src/shared/SettingData.ts @@ -141,13 +141,13 @@ export class JSONTextSettings { } export class FormSettings { - private keymaps: FormKeymaps; + public readonly keymaps: FormKeymaps; - private search: FormSearch; + public readonly search: FormSearch; - private properties: Properties; + public readonly properties: Properties; - private blacklist: Blacklist; + public readonly blacklist: Blacklist; constructor( keymaps: FormKeymaps, @@ -210,7 +210,7 @@ export class FormSettings { keymaps: ReturnType; search: ReturnType; properties: ReturnType; - blacklist: string[]; + blacklist: ReturnType; } { return { keymaps: this.keymaps.toJSON(), diff --git a/src/shared/settings/Properties.ts b/src/shared/settings/Properties.ts index 1bc4c7f..63ff991 100644 --- a/src/shared/settings/Properties.ts +++ b/src/shared/settings/Properties.ts @@ -59,7 +59,7 @@ export default class Properties { hintchars?: string; smoothscroll?: boolean; complete?: string; - }) { + } = {}) { this.hintchars = hintchars || defaultValues.hintchars; this.smoothscroll = smoothscroll || defaultValues.smoothscroll; this.complete = complete || defaultValues.complete; -- cgit v1.2.3 From 532eeb5a1d9712676a9b3730bb7b27134c57831b Mon Sep 17 00:00:00 2001 From: Shin'ya UEOKA Date: Mon, 7 Oct 2019 12:02:02 +0000 Subject: Rename valueOf to fromJSON --- .../repositories/PersistentSettingRepository.ts | 2 +- src/settings/components/form/KeymapsForm.tsx | 2 +- src/settings/components/form/SearchForm.tsx | 4 ++-- src/settings/components/index.tsx | 2 +- src/settings/storage.ts | 2 +- src/shared/SettingData.ts | 24 +++++++++++----------- test/settings/components/form/KeymapsForm.test.tsx | 4 ++-- .../components/form/SearchEngineForm.test.tsx | 8 ++++---- test/shared/SettingData.test.ts | 12 +++++------ 9 files changed, 30 insertions(+), 30 deletions(-) (limited to 'src/settings') diff --git a/src/background/repositories/PersistentSettingRepository.ts b/src/background/repositories/PersistentSettingRepository.ts index 927bce9..e3b78b3 100644 --- a/src/background/repositories/PersistentSettingRepository.ts +++ b/src/background/repositories/PersistentSettingRepository.ts @@ -8,7 +8,7 @@ export default class SettingRepository { if (!settings) { return null; } - return SettingData.valueOf(settings as any); + return SettingData.fromJSON(settings as any); } } diff --git a/src/settings/components/form/KeymapsForm.tsx b/src/settings/components/form/KeymapsForm.tsx index 3cba0c0..94934ae 100644 --- a/src/settings/components/form/KeymapsForm.tsx +++ b/src/settings/components/form/KeymapsForm.tsx @@ -12,7 +12,7 @@ interface Props { class KeymapsForm extends React.Component { public static defaultProps: Props = { - value: FormKeymaps.valueOf({}), + value: FormKeymaps.fromJSON({}), onChange: () => {}, onBlur: () => {}, }; diff --git a/src/settings/components/form/SearchForm.tsx b/src/settings/components/form/SearchForm.tsx index 6ba6cfb..0aaf6fd 100644 --- a/src/settings/components/form/SearchForm.tsx +++ b/src/settings/components/form/SearchForm.tsx @@ -12,7 +12,7 @@ interface Props { class SearchForm extends React.Component { public static defaultProps: Props = { - value: FormSearch.valueOf({ default: '', engines: []}), + value: FormSearch.fromJSON({ default: '', engines: []}), onChange: () => {}, onBlur: () => {}, }; @@ -81,7 +81,7 @@ class SearchForm extends React.Component { } } - this.props.onChange(FormSearch.valueOf(next)); + this.props.onChange(FormSearch.fromJSON(next)); if (name === 'delete' || name === 'default') { this.props.onBlur(); } diff --git a/src/settings/components/index.tsx b/src/settings/components/index.tsx index 586f0f0..160dd9c 100644 --- a/src/settings/components/index.tsx +++ b/src/settings/components/index.tsx @@ -133,7 +133,7 @@ class SettingsComponent extends React.Component { let data = new SettingData({ source: this.props.source, form: (this.props.form as FormSettings).buildWithSearch( - FormSearch.valueOf(value)), + FormSearch.fromJSON(value)), }); this.props.dispatch(settingActions.set(data)); } diff --git a/src/settings/storage.ts b/src/settings/storage.ts index 32b6351..2a983df 100644 --- a/src/settings/storage.ts +++ b/src/settings/storage.ts @@ -6,7 +6,7 @@ export const load = async(): Promise => { return DefaultSettingData; } try { - return SettingData.valueOf(settings as any); + return SettingData.fromJSON(settings as any); } catch (e) { console.error('unable to load settings', e); return DefaultSettingData; diff --git a/src/shared/SettingData.ts b/src/shared/SettingData.ts index b19a756..532570e 100644 --- a/src/shared/SettingData.ts +++ b/src/shared/SettingData.ts @@ -6,9 +6,9 @@ import Properties from './settings/Properties'; import Blacklist from './settings/Blacklist'; 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; } @@ -38,7 +38,7 @@ export class FormKeymaps { return new FormKeymaps(newData); } - static valueOf(o: ReturnType): FormKeymaps { + static fromJSON(o: ReturnType): FormKeymaps { let data: {[op: string]: string} = {}; for (let op of Object.keys(o)) { data[op] = o[op] as string; @@ -65,9 +65,9 @@ export class FormKeymaps { } export class FormSearch { - private default: string; + private readonly default: string; - private engines: string[][]; + private readonly engines: string[][]; constructor(defaultEngine: string, engines: string[][]) { this.default = defaultEngine; @@ -92,7 +92,7 @@ export class FormSearch { }; } - static valueOf(o: ReturnType): FormSearch { + static fromJSON(o: ReturnType): FormSearch { if (!Object.prototype.hasOwnProperty.call(o, 'default')) { throw new TypeError(`"default" field not set`); } @@ -220,15 +220,15 @@ export class FormSettings { }; } - static valueOf(o: ReturnType): FormSettings { + static fromJSON(o: ReturnType): FormSettings { for (let name of ['keymaps', 'search', 'properties', 'blacklist']) { if (!Object.prototype.hasOwnProperty.call(o, name)) { throw new Error(`"${name}" field not set`); } } return new FormSettings( - FormKeymaps.valueOf(o.keymaps), - FormSearch.valueOf(o.search), + FormKeymaps.fromJSON(o.keymaps), + FormSearch.fromJSON(o.search), Properties.fromJSON(o.properties), Blacklist.fromJSON(o.blacklist), ); @@ -311,7 +311,7 @@ export default class SettingData { throw new Error(`unknown settings source: ${this.source}`); } - static valueOf(o: { + static fromJSON(o: { source: string; json?: string; form?: ReturnType; @@ -326,7 +326,7 @@ export default class SettingData { case SettingSource.Form: return new SettingData({ source: o.source, - form: FormSettings.valueOf( + form: FormSettings.fromJSON( o.form as ReturnType), }); } @@ -334,7 +334,7 @@ export default class SettingData { } } -export const DefaultSettingData: SettingData = SettingData.valueOf({ +export const DefaultSettingData: SettingData = SettingData.fromJSON({ source: 'json', json: DefaultSettingJSONText, }); diff --git a/test/settings/components/form/KeymapsForm.test.tsx b/test/settings/components/form/KeymapsForm.test.tsx index dc2322b..1d1e77c 100644 --- a/test/settings/components/form/KeymapsForm.test.tsx +++ b/test/settings/components/form/KeymapsForm.test.tsx @@ -9,7 +9,7 @@ import { expect } from 'chai'; describe("settings/form/KeymapsForm", () => { describe('render', () => { it('renders keymap fields', () => { - let root = ReactTestRenderer.create().root @@ -48,7 +48,7 @@ describe("settings/form/KeymapsForm", () => { it('invokes onChange event on edit', (done) => { ReactTestUtils.act(() => { ReactDOM.render( { describe('render', () => { it('renders SearchForm', () => { - let root = ReactTestRenderer.create().root; @@ -41,7 +41,7 @@ describe("settings/form/SearchForm", () => { it('invokes onChange event on edit', (done) => { ReactTestUtils.act(() => { ReactDOM.render( { it('invokes onChange event on delete', (done) => { ReactTestUtils.act(() => { - ReactDOM.render( { it('invokes onChange event on add', (done) => { ReactTestUtils.act(() => { - ReactDOM.render( { 'scroll.home': '0', }; - let keymaps = FormKeymaps.valueOf(data).toKeymaps().toJSON(); + let keymaps = FormKeymaps.fromJSON(data).toKeymaps().toJSON(); expect(keymaps).to.deep.equal({ 'j': { type: 'scroll.vertically', count: 1 }, '0': { type: 'scroll.home' }, @@ -118,7 +118,7 @@ describe('shared/SettingData', () => { blacklist: [] }; - let settings = FormSettings.valueOf(data).toSettings(); + let settings = FormSettings.fromJSON(data).toSettings(); expect({ keymaps: settings.keymaps.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.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.form).to.deep.equal({ 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'); }); @@ -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'); }); }); -- cgit v1.2.3