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 #1029 from riganti/feature/dictionary-translations
Added JavaScript translations for Dictionary methods
- Loading branch information
Showing
5 changed files
with
167 additions
and
27 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
61 changes: 46 additions & 15 deletions
61
src/DotVVM.Framework/Resources/Scripts/collections/dictionaryHelper.ts
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 |
---|---|---|
@@ -1,27 +1,58 @@ | ||
type Dictionary<Key, Value> = { Key: Key, Value: Value }[]; | ||
|
||
export function clear(observable: any): void { | ||
observable.setState([]); | ||
} | ||
|
||
export function containsKey<Key, Value>(dictionary: Dictionary<Key, Value>, identifier: Key): boolean { | ||
return getKeyValueIndex(dictionary, identifier) !== null; | ||
} | ||
|
||
export function getItem<Key, Value>(dictionary: Dictionary<Key, Value>, identifier: Key): Value { | ||
for (let index = 0; index < dictionary.length; index++) { | ||
let keyValuePair = ko.unwrap(dictionary[index]); | ||
if (ko.unwrap(keyValuePair.Key) == identifier) { | ||
return keyValuePair.Value; | ||
} | ||
const index = getKeyValueIndex(dictionary, identifier); | ||
if (index === null) { | ||
throw Error("Provided key \"" + identifier + "\" is not present in the dictionary!"); | ||
} | ||
|
||
throw Error("Provided key \"" + identifier + "\" is not present in the dictionary!"); | ||
return dictionary[index].Value; | ||
} | ||
|
||
export function remove<Key, Value>(observable: any, identifier: Key): boolean { | ||
let dictionary = [...observable.state]; | ||
const index = getKeyValueIndex(dictionary, identifier); | ||
|
||
if (index === null) { | ||
return false; | ||
} | ||
else { | ||
dictionary.splice(index, 1); | ||
observable.setState(dictionary); | ||
return true; | ||
} | ||
} | ||
|
||
export function setItem<Key, Value>(observable: any, identifier: Key, value: Value): void { | ||
const dictionary = [...observable.state] | ||
for (let index = 0; index < dictionary.length; index++) { | ||
const dictionary = [...observable.state]; | ||
const index = getKeyValueIndex(dictionary, identifier); | ||
|
||
if (index !== null) { | ||
let keyValuePair = dictionary[index]; | ||
if (keyValuePair.Key == identifier) { | ||
dictionary[index] = { Value: value, Key: keyValuePair.Key } | ||
observable.setState(dictionary) | ||
return; | ||
dictionary[index] = { Key: keyValuePair.Key, Value: value }; | ||
observable.setState(dictionary); | ||
} | ||
else { | ||
dictionary.push({ Key: identifier, Value: value }); | ||
observable.setState(dictionary); | ||
} | ||
} | ||
|
||
function getKeyValueIndex<Key, Value>(dictionary: Dictionary<Key, Value>, identifier: Key): number | null { | ||
for (let index = 0; index < dictionary.length; index++) { | ||
let keyValuePair = ko.unwrap(dictionary[index]); | ||
if (ko.unwrap(keyValuePair.Key) == identifier) { | ||
return index; | ||
} | ||
} | ||
// Create new record if we did not find provided key | ||
dictionary.push({ "Key": identifier, "Value": value }); | ||
observable.setState(dictionary); | ||
|
||
return null; | ||
} |
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