Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
@blueworld/dictionarray
Advanced tools
An indexed array for fast data access
Simply install the module via NPM:
npm i @blueworld/dictionarray -S
Import the TypeScript module into your project files:
import DictionArray from "@blueworld/dictionarray";
The constructor of the generic DictionArray class receives a type and an object of index functions that we want to build up.
Let's consider the following example:
interface Test {
id: number;
text: string;
}
const d = new DictionArray<Test>({
identifier: (elem: Test) => {
return elem.id.toString();
}
});
We instantiate a new DictionArray of type Test
. We also specify an index with the name identifier
. The index function extracts the property id
of the element of type Test
. Everytime a new element is added or removed to and from the DictionArray, the index for identifier
is updated by running the index function across all elements of the DictionArray.
Note: The index function only accepts strings as return values. If you want your numeric id
property to be indexed, you have to cast it into a string first, as seen in the example above.
This is due to the fact that the extracted property per element of object T
will become a key in an ECMAScript object internally.
The push method works analogous to the standard ECMAScript Array.push() method. It takes n-many objects of type T
and returns the new length of the array.
Example:
d.push({
id: 123,
text: "Test"
});
Triggers reindex: ✅
This is where the magic of the DictionArray happens. We can push objects onto the array-part of the DictionArray, but what if we want to find a single element in the data structure without looping the whole array? Glad you're asking! Since we declared an index of name identifier
in the Constructor-section above, we can use this index to lookup elements O(1) runtime.
const pos: number[] = d.lookup("identifier", "123");
The contant pos
will now contain the positions of the objects of type T
with the identifier of value 123
within the dictionarray.
Triggers reindex: ❌
To read an object at a certain position within the DictionArray, use the get()
method.
Example:
const obj: Test = d.get(0);
Will return the first object within the DictionArray.
Triggers reindex: ❌
count()
returns the length of the array within the DictionArray.
Example:
const length: number = d.count();
Triggers reindex: ❌
The retrieve all elements of the DictionArray in array form, use the all()
function:
const elements: Test[] = d.all();
Triggers reindex: ❌
To remove all elements from the DictionArray use the clear()
method:
d.clear();
Triggers reindex: ❌ (well, the index is completely cleared)
Proxy function for the map()
function of the underlaying array within the DictionArray.
Example:
const result = d.map((item) => {
return item.text;
});
Triggers reindex: ❌
Proxy function for the filter()
function of the underlaying array within the DictionArray.
Example:
const result: Test[] = d.filter((item) => {
return item.text.length > 2;
});
Triggers reindex: ❌
The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements.
Works analogous to the ECMAScript splice() function.
Example:
d.splice(0, 1);
This removes the first item from the array.
Triggers reindex: ✅
FAQs
An indexed array for fast data access
We found that @blueworld/dictionarray demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.