| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
A map where multiple keys point to a single value, and the removal of a single key removes all matching key-value pairs.
Install from NPM with
$ npm install --save @jsdsl/alias-map
First, import the AliasMap class and initialize a new AliasMap.
import { AliasMap } from "@jsdsl/aliasmap";
let aliasMap: AliasMap<string, string> = new AliasMap<string, string>();Then, we can set key-value pairs. It is important to note that the inline documentation often refers to 'keys' as 'aliases'.
aliasMap.set("cow", "moo");
aliasMap.set("pig", "oink");Now that we have existing key-value pairs in our map, we can optionally add aliases to the existing keys.
aliasMap.addAliases("cow", "bovine");
aliasMap.addAliases("pig", "hog");Alias addition also works transitively - aliases can be added via other aliases. That is to say that you can now do the following:
aliasMap.addAliases("hog", "piglet");Now we can retrieve the values we have added via any of the aliases that have been established for a given value.
aliasMap.get("cow"); // --> moo
aliasMap.get("bovine"); // --> moo
aliasMap.get("pig"); // --> oink
aliasMap.get("hog"); // --> oink
aliasMap.get("piglet"); // --> oinkWant to check if a given alias exists in the map?
aliasMap.has("bovine"); // --> true
aliasMap.has("lamb"); // --> false
aliasMap.has("pig"); // --> trueIf you need to modify the value that a set of aliases point to, you don't need to modify every alias for the value. Instead, you can modify the value more cleanly.
aliasMap.modify("cow", "mooooo");Finding all of the related aliases for a given input is also easy. A second boolean parameter can be provided to determine whether or not the output array contains the input alias.
aliasMap.listAliases("cow"); // --> ["cow", "bovine"]
aliasMap.listAliases("hog", true); // --> ["hog", "pig", "piglet"]
aliasMap.listAliases("hog", false); // --> ["pig", "piglet"]On a related note we can also find the number of related aliases for a given input alias.
aliasMap.numberOfAliasesFor("cow"); // --> 2
aliasMap.numberOfAliasesFor("piglet"); // --> 3Finding the size of the map is fairly straight-forward, returning the number of values that a given map holds (NOT the number of aliases).
aliasMap.size(); // --> 2We can choose to either remove singular aliases or entire values (which implies the removal of all of the aliases for the given value).
aliasMap.removeAlias("cow"); // --> moo
aliasMap.removeValue("hog"); // --> oink
// The "oink" value no longer exists in the map.
// The map now only holds one key-value pair:
// "bovine" --> "moo"If we continue to remove all of the aliases for a given value, the value will be automatically removed from the map as well.
aliasMap.removeAlias("bovine"); // --> moo
// The "moo" value no longer exists in the map.If we ever want to completely 'empty' the map, we can use the #clear operation.
// The map is actually already empty, but for good measure...
aliasMap.clear();Initialization is done with an no-arguments call to the constructor.
Parameters:
Returns A newly initialized, empty AliasMap.
public constructor() { ... }The first generic argument corresponds to the type of the keys/aliases for the map, while the second generic argument corresponds with the type of the values for the map.
let aliasMap: AliasMap<AliasType, ValueType> = new AliasMap<AliasType, ValueType>();Returns the value associated with a given alias, or undefined if the alias does not exist in the map.
Parameters:
Returns The value associated with the provided alias, or undefined if the alias does not exist in the map.
public get(key: A): V | undefined { ... }Associates the provided alias with the provided value, returning true if and only if, after this operation, calling AliasMap#get with the provided alias will return the value provided.
Note that this method returning 'true' does not necessarily mean that the internal state of the map was modified, as it is possible that the provided key-value pair already to have existed. As such, the return condition of this method was satisfied and the method therefore returned true.
Parameters:
Returns true if and only if, after this operation, calling AliasMap#get with the provided alias will return the value provided.
public set(key: A, value: V, force: boolean = false): boolean { ... }Adds an alias to an existing value via an existing alias, returning true if and only if, after this operation, calling AliasMap#get with the provided alias will return the value provided.
Parameters:
Returns true if and only if, after this operation, calling AliasMap#get with the provided alias will return the value provided.
public addAliases(existingKey: A, newKey: A, force: boolean = false): boolean { ... }Returns true if the provided alias has an associated value in this map.
Parameters:
Returns true if the provided alias has an associated value in this map.
public has(key: A): boolean { ... }Modifies the value of a given alias and all of it's associated aliases, returning the value that was displaced or undefined if the provided alias did not exist in the map.
Parameters:
Returns The value that was displaced or undefined if the provided alias did not exist in the map.
public modify(key: A, value: V): V | undefined { ... }Attempts to remove an alias from this map, returning the value associated with the removed alias if one existed, otherwise returning undefined.
Note that the removal of the last existing alias for a given value implies the removal of the value from the map.
Parameters:
Returns The value associated with the removed alias if one existed, otherwise undefined.
public removeAlias(key: A): V | undefined { ... }Attempts to remove the value that is associated with the provided alias from the map, returning the aforementioned value if the provided alias existed within the map, otherwise returning undefined.
Note that this implies the removal of all of the aliases for the removed value.
Parameters:
Returns The value that was removed from the map if one existed, otherwise undefined.
public removeValue(key: A): V | undefined { ... }Returns an array of aliases associated with the provided input alias, or undefined if the input array does not exist within the map.
A second parameter, a boolean, can be set, determining whether or not the input alias should be included in the returned array of aliases. If a truthy value is provided, the input alias will be included, otherwise the input alias will be removed from the returned array. Note that if a truthy value is provided, this method is operates in O(1) time, whereas if a falsy value is provided, this method runs in O(k) time where k is the number of aliases associated with the input alias.
Parameters:
Returns An array of aliases that are associated/equivalent to the provided input alias.
public listAliases(alias: A, includeProvidedAlias: boolean = true): A[] | undefined { ... }Returns the number of aliases that exist in the map for the input alias, or zero if the alias does not exist within the map.
A second parameter, a boolean, can be set, determining whether or not the input alias should be counted in the returned value. Although this method internally uses AliasMap#listAliases, and despite the O(k) nature of the aforementioned method, this second parameter will not change the running speed of this method, which will always be O(1).
Parameters:
Returns The number of aliases that exist in the map for the input alias, or zero if the alias does not exist within the map.
public numberOfAliasesFor(alias: A, includeProvidedAlias: boolean = true): number { ... }Returns the number of values stored in this map.
Note that the value that this method returns is in no way associated with the number of aliases that exist for any given value within the map.
Parameters:
Returns The number of values stored in this map.
public size(): number { ... }Resets this map to an empty state, removing all of its stored key-value pairs.
Parameters:
Returns Void.
public clear(): void { ... }@jsdsl/alias-map is made available under the GNU General Public License v3.
Copyright (C) 2019 Trevor Sears
| Back | FazBrowse Home | New Git URL |