Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
recollections
Advanced tools
Collections like Array, Set, Map, with operators and observers, for reactive applications
Author: Ben Bucksch
JS Collections provides a coherent set of collection/list classes. They form a powerful, yet lean system to design component APIs and plug different modules together between backend logic and UI. Like LEGO Technic. It simplifes application code with list operators and automatic updates.
This is mostly about 3 aspects:
These aspects work together: Operation results are observable and change when the underlying operand collections change. Operations can be chained. All collection types support all operations.
That means you can have a shopItems
collection defined as the result of installedItems
subtracted from availableItems
. When you show the shopItems
, the user sees only those items that are not yet installed. Now, as soon as an item is added to installedItems
, it automatically disappears from the shop UI - immediately, without you having to write any extra code in the UI to support these updates. You don't have to install observers to installedItems
, because the subtract operator already does that. If your list UI component observes list changes using the collection API here, you don't need any UI update wiring at all. Its all calculated and updated automatically. The installedItems
can be managed by a backend module - completely independently from the UI. This allows you to decouple logic from the UI.
The real value comes from a coherent API and base functionality that can be used in other APIs. It removes the need for getItemList(), addItem()/removeItem(), addObserver()/removeObserver(), load(callback) functions in your API, and allows modules to work together. If each API uses a slightly different API to add/remove items, not only does the programmer have to learn each API, but he also has to do the plumbing between the components all manually. This gets particularly tedious as there need to be dynamic updates from one component to another, e.g. data to UI or pref dialog to main UI.
Show only those items which are not already purchased, i.e. only offer new stuff
var availableItems = new ArrayColl([ itemA, itemB ]);
var installedItems = getInstalledItems(path);
var shopItems = availableItems.subtract(installedItems);
var listbox = E("itemsList");
listbox.showCollection(shopItems);
That's all. When items are added to availableItems
later, they automatically appear in the list UI, unless they are in installedItems
. shopItems
will be automatically updated and displayed, without further application code.
That's because the subtract operator automatically subscribed to changes in availableItems
. If you then later do availableItems.add(itemC)
, the subtract operator would check whether the same items is in installedItems
, and only if it's not, it would add it to shopItems
. listbox.showCollection()
in turn automatically subscribed to changes in shopItems
, gets notified about the new item there, and shows it. All of that would happen just with the above code lines, there is no additional code needed to follow updates.
Of course, if you wanted to show both availableItems
and installedItems
in the UI, you would do add()
instead of substract()
.
This means that you don't need additional wiring to make the UI update after the user (or server) did add/remove item actions, you only need to update the underlying lists.
id
property matches, it's the same item" and "sort on name
property" or "if a.name > b.name, then a > b"allItems = merge(availableItems, installedItems);
listbox.showList(allItems)
The classes are standing alone, they do not change JS Array or Object types.
(extract, full docs will be in module)
Base class for all lists.
add(item)
Object
} any JS objectremove(item)
Object
} any JS objectaddAll(coll)
coll
to this list.coll
changes.add
.Collection or JS Array
}removeAll(coll)
coll
from this listCollection or JS Array
}clear()
get length()
Integer
} (always >= 0)get isEmpty()
Boolean
}contains(item)
Boolean
}contents()
Array
} new JS array with all itemsforEach(func)
Boolean
}filter(filterFunc)
Function(item)
}Collection of items
} where filterFunc
returned true
find(filterFunc)
Function(item)
}Object
} for which filterFunc
returned true
map(mapFunc)
mapFunc
.Collection of Object
} whereby {Object
} is the result of mapFunc()
concat(otherColl)
merge(otherColl)
subtract(collSubtract)
inCommonColl(otherColl)
notInCommonColl(otherColl)
sortColl(sortFunc)
registerObserver(observer)
CollectionObserver
}unregisterObserver(observer)
registerObserver
CollectionObserver
}A collection where entries have a key or label or index.
Examples of subclasses: Array (key = index), Map
set(key, item)
key
get(key)
key
undefined
.removeKey(key)
containsKey(key)
Boolean
}getKeyForValue(value)
value
and if found, returns the (first) key for it.To listen to collection changes, you need to implement this interface.
added(item, list)
Object
} the removed itemCollection
} the observed list. convenience only.removed(item, coll)
Object
} the removed itemCollection
} the observed list. convenience only.To create a collection, instantiate one of these.
ArrayColl
KeyValueCollection
based on a JS Array.Array
} (optional) init the collection with these valuesSetColl
Collection
which can hold each object only once.MapColl
KeyValueCollection
which can hold each object only once.DOMColl
Collection
which wraps a DOMNodeList.DOMNodeList
}add, subtract, and, xor - compare Set theory and sort
All operators observe the original collections they are constructed from, and adapt the result based on changes, and notify any observers that are registered on the operator result collection.
concatColl(coll1, coll2)
Collection
}Collection
}Collection
} Preserves ordermergeColl(coll1, coll2)
Collection
}Collection
}Collection
} Does not preserve order.subtractColl(collBase, collSubtract)
Collection
}Collection
}Collection
} Preserves order of collBase.inCommonColl(coll1, coll2)
Collection
}Collection
}Collection
} Does not preserve order.notInCommonColl(coll1, coll2)
Collection
}Collection
}Collection
} Does not preserve order.sortColl(coll, sortFunc)
sortFunc
.Collection
}Item
}, b {Item
}) returns {Boolean
} a > bCollection
}DONE
TODO
Code / download
git clone https://github.com/benbucksch/jscollection
How to specify identity and sorting for items, e.g. "if id
property matches, it's the same item" and "sort on name
property" or "if a.name > b.name, then a > b"
Options:
base class for items -- convenient, but doesn't allow to collect objects not supporting this API, also doesn't allow to specify different sorting based on situation
Operators and Set and Ordered* collections take functions that can compare the objects -- cumbersome, because it needs to be specified for every use
specify id
and sortBy
properties, or -- more convenient:
specify isSameObject()
and isGreaterThan()
functions - more flexible
FAQs
Collections like Array, Set, Map, with operators and observers, for reactive applications
The npm package recollections receives a total of 0 weekly downloads. As such, recollections popularity was classified as not popular.
We found that recollections 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.