-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1727 from CentreForDigitalHumanities/feature/n-gr…
…am-update Clean up routing behaviour in ngram component
- Loading branch information
Showing
10 changed files
with
245 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { SimpleStore } from '../store/simple-store'; | ||
import { RouterStoreService } from '../store/router-store.service'; | ||
import { NgramParameters, NgramSettings } from './ngram'; | ||
|
||
describe('NgramParameters', ()=> { | ||
let store: RouterStoreService = new SimpleStore() as any; | ||
let ngramParameters: NgramParameters; | ||
const testState = { | ||
size: 3, | ||
positions: 'first', | ||
freqCompensation: true, | ||
analysis: 'clean', | ||
maxDocuments: 100, | ||
numberOfNgrams: 20, | ||
} as NgramSettings; | ||
const testParams = {ngramSettings: 's:3,p:first,c:true,a:clean,m:100,n:20'} | ||
|
||
beforeEach(() => { | ||
ngramParameters = new NgramParameters(store); | ||
}); | ||
|
||
it('should correctly convert internal state to a route parameter', () => { | ||
const params = ngramParameters.stateToStore(testState); | ||
expect(params).toEqual(testParams); | ||
}); | ||
|
||
it('should correctly convert a route parameter to an internal state', () => { | ||
const state = ngramParameters.storeToState(testParams); | ||
expect(state).toEqual(testState); | ||
}); | ||
|
||
it('should return default values if no relevant route parameter is present', () => { | ||
const defaultSettings = { | ||
size: 2, | ||
positions: 'any', | ||
freqCompensation: false, | ||
analysis: 'none', | ||
maxDocuments: 50, | ||
numberOfNgrams: 10, | ||
} as NgramSettings; | ||
const state = ngramParameters.storeToState({irrelevant: 'parameter'}) | ||
expect(state).toEqual(defaultSettings); | ||
}) | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Params } from '@angular/router'; | ||
import * as _ from 'lodash'; | ||
|
||
import { StoreSync } from '../store/store-sync'; | ||
import { Store } from '../store/types'; | ||
|
||
export interface NgramSettings { | ||
size: number; | ||
positions: string; | ||
freqCompensation: boolean; | ||
analysis: string; | ||
maxDocuments: number; | ||
numberOfNgrams: number; | ||
} | ||
|
||
export class NgramParameters extends StoreSync<NgramSettings> { | ||
|
||
keysInStore = ['ngramSettings']; | ||
|
||
constructor(store: Store) { | ||
super(store); | ||
this.connectToStore(); | ||
} | ||
|
||
stringifyNgramSettings(state: NgramSettings): string { | ||
return [`s:${state.size}`,`p:${state.positions}`,`c:${state.freqCompensation}`, | ||
`a:${state.analysis}`,`m:${state.maxDocuments}`,`n:${state.numberOfNgrams}`].join(',') | ||
} | ||
|
||
stateToStore(state: NgramSettings): Params { | ||
return { ngramSettings: this.stringifyNgramSettings(state)} | ||
} | ||
|
||
storeToState(params: Params): NgramSettings { | ||
if (_.has(params, 'ngramSettings')) { | ||
const stringComponents = params['ngramSettings'].split(','); | ||
return { | ||
size: parseInt(this.findSetting('s', stringComponents), 10), | ||
positions: this.findSetting('p', stringComponents), | ||
freqCompensation: this.findSetting('c', stringComponents) === 'true', | ||
analysis: this.findSetting('a', stringComponents), | ||
maxDocuments: parseInt(this.findSetting('m', stringComponents), 10), | ||
numberOfNgrams: parseInt(this.findSetting('n', stringComponents), 10), | ||
} | ||
} | ||
return { | ||
size: 2, | ||
positions: 'any', | ||
freqCompensation: false, | ||
analysis: 'none', | ||
maxDocuments: 50, | ||
numberOfNgrams: 10, | ||
} as NgramSettings; | ||
} | ||
|
||
findSetting(abbreviation: string, stringComponents: string[]): string | undefined{ | ||
const setting = stringComponents.find(s => s[0] === abbreviation); | ||
return setting.split(':')[1]; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.