@dochne/joy
v0.1.0
Published
Ruby-like extensions for TypeScript native objects
Maintainers
Readme
Joy
Ruby-like extensions for TypeScript native objects. This library adds useful methods to Array, String, Number, Set, and Object prototypes to provide a more expressive, Ruby-inspired API.
Installation
npm install @dochne/joyUsage
Simply import the library at the entry point of your application to enable all extensions:
import "@dochne/joy";
// Now you can use the extended methods
[1, 2, 3].first(); // 1
[1, 2, 3].last(); // 3
(5).times((i) => console.log(i)); // logs 0, 1, 2, 3, 4
"hello".insert(1, "a"); // "hallo"API
Array Extensions
first()- Returns the first elementlast()- Returns the last elementeachCons(n)- Returns consecutive n-element subarrayseachWithObject(initial, block)- Iterates with an accumulator objectfilterMap(block)- Maps and filters in one passgroupBy(block)- Groups elements by a key functionintersect(items)- Returns intersection with another collectionmax()- Returns maximum value (for number arrays)min()- Returns minimum value (for number arrays)product()- Returns product of all numbers, or cartesian product with arrayssortBy(block)- Sorts by a computed valuesum(block?)- Returns sum of numberstap(block)- Executes block and returns selfthen(block)- Pipes array into a functiontranspose()- Transposes a 2D arrayunion(...arrays)- Returns union of arrays
Number Extensions
times(block)- Executes block n timesthen(block)- Pipes number into a function
String Extensions
insert(pos, char)- Inserts character at positionremove(pos)- Removes character at positionthen(block)- Pipes string into a function
Set Extensions
filter(block)- Filters set elementsaddMany(...items)- Adds multiple items at once
Object Extensions
tap(block)- Executes block and returns selfchain(block)- Pipes object into a function
Development
# Install dependencies
npm install
# Build the library
npm run build
# The compiled output will be in the dist/ directoryLicense
MIT
