Gathering detailed insights and metrics for swiss-army-knifey
Gathering detailed insights and metrics for swiss-army-knifey
npm install swiss-army-knifey
Typescript
Module System
Node Version
NPM Version
TypeScript (100%)
Total Downloads
10,692
Last Day
7
Last Week
34
Last Month
96
Last Year
1,723
67 Commits
2 Watching
9 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
1.32.2
Package Id
swiss-army-knifey@1.32.2
Unpacked Size
467.17 kB
Size
76.11 kB
File Count
179
NPM Version
10.9.0
Node Version
23.2.0
Publised On
04 Dec 2024
Cumulative downloads
Total Downloads
Last day
133.3%
7
Compared to previous day
Last week
61.9%
34
Compared to previous week
Last month
-67.7%
96
Compared to previous month
Last year
-59.5%
1,723
Compared to previous year
Tools you could have gotten somewhere else, but you still decided to go this way.
1import { createEventEmitter } from './utils/createEventEmitter'; 2 3// Create an event emitter instance 4const emitter = createEventEmitter(); 5 6// Subscribe to events 7emitter.on('userLoggedIn', (user) => { 8 console.log('User logged in:', user); 9}); 10 11// Subscribe to all events using wildcard 12emitter.on('*', (data) => { 13 console.log('Any event occurred:', data); 14}); 15 16// Emit events 17emitter.emit('userLoggedIn', { id: 1, name: 'John' }); 18 19// Unsubscribe from events 20const callback = (data) => console.log(data); 21emitter.on('notification', callback); 22emitter.off('notification', callback); 23 24// Remove all callbacks for an event 25emitter.offAll('notification');
1import { createLogger } from './utils/logger'; 2import { createEventEmitter } from './utils/createEventEmitter'; 3 4// Create a logger with custom settings 5const logger = createLogger({ 6 LOGGER_DEBUG: true, // Enable debug logs 7 LOGGER_LOG: true // Enable warning and error logs 8}); 9 10// Create an event emitter with custom logger 11const emitter = createEventEmitter({ logger }); 12 13// Now all events will be logged according to your settings 14emitter.emit('test', 'Hello, World!'); 15// [2023-07-20T10:00:00.000Z] [LOG] Emitting event "test" with params: Hello, World!
The event emitter automatically catches and logs errors in event handlers:
1const emitter = createEventEmitter(); 2 3// This handler will throw an error 4emitter.on('buggy', () => { 5 throw new Error('Something went wrong!'); 6}); 7 8// The error will be caught and logged, other handlers will still execute 9emitter.emit('buggy');
TypeScript interfaces ensure type safety:
1interface UserEvents { 2 userLoggedIn: { id: number; name: string }; 3 userLoggedOut: { id: number }; 4} 5 6// Create a type-safe event emitter 7const emitter = createEventEmitter<UserEvents>(); 8 9// TypeScript will ensure correct event names and payload types 10emitter.on('userLoggedIn', (user) => { 11 console.log(user.name); // TypeScript knows `name` exists 12}); 13 14// This would cause a type error 15emitter.emit('userLoggedIn', { id: 1 }); // Error: missing 'name' property
Added typings to composeWithPromise
1 it('should await and return a composed result', async function() { 2 const paramToProp = (param: string) => ({ param }); 3 const sleepingSentence = (s: number) => async ({ param }: { param: string }) => { 4 const d1 = new Date(); 5 await sleep(s, TimeUnit.second); 6 const d2 = new Date(); 7 return `a_[${param}], slept for: ${d2.getTime() - d1.getTime()}`; 8 }; 9 const toUp = async (param: string) => param.toUpperCase(); 10 const removeStart = async (param: string) => param.slice(1); 11 const composed = composeWithPromise( 12 removeStart, 13 sleepingSentence(1), 14 paramToProp, 15 toUp, 16 sleepingSentence(2), 17 paramToProp 18 ); 19 return expect(composed('hey')).to.eventually.match(/_\[A_\[HEY\], SLEPT FOR: \d{4}\], slept for: \d{3,4}$/g); 20 });
Very simple sleep:
1import { sleep, TimeUnits } from 'swiss-army-kinfey'; 2 3(async function main() { 4 5while (1) { 6 await sleep(1000); 7 console.log('Sleeping forever, in 1000 milliseconds invervals') 8} 9 10while (1) { 11 await sleep(1, TimeUnit.hours); 12 console.log('Sleeping forever, in 1 hour invervals') 13} 14 15})(); 16
The second while
will never get executed, but you get the idea.
Need to call a function between sleep intervals ? Use sleepForCallback
or its fixed unit equivalents:
sleepMillisecondsCallback
sleepSecondsCallback
sleepMinutesCallback
sleepHoursCallback
sleepDaysCallback
1import { sleepForCallback, TimeUnit } from 'swiss-army-knifey'; 2 3(async function main() { 4 5 await sleepForCallback(TimeUnit.seconds)(6, (current: number, total: number, unit: TimeUnit) => { 6 console.log(`Sleeping for ${total} ${unit}, but calling this on every step: ${current}${unit.substring(0,1)}`); 7 }); 8 9})();
Output :
1Sleeping for 6 seconds, but calling this on every step: 0s 2Sleeping for 6 seconds, but calling this on every step: 1s 3Sleeping for 6 seconds, but calling this on every step: 2s 4Sleeping for 6 seconds, but calling this on every step: 3s 5Sleeping for 6 seconds, but calling this on every step: 4s 6Sleeping for 6 seconds, but calling this on every step: 5s
Or equivalently :
1import { sleepSecondsCallback, TimeUnit } from 'swiss-army-knifey'; 2 3(async function main() { 4 5 await sleepSecondsCallback(6, (current: number, total: number, unit: TimeUnit) => { 6 console.log(`Sleeping for ${total} ${unit}, but calling this on every step: ${current}${unit.substring(0,1)}`); 7 }); 8 9})();
Produces the same output as above
Ever wondered if you could get rid of imperative for
/while
loops using arrays ?
1import { getArrayRange } from 'swiss-army-knifey'; 2getArrayRange(-4, 5); // [-4, -3, -2, -1, 0, 1, 2, 3, 4, 5] 3getArrayRange(0, 9); // [0, 1, 2, 3, 4, 5, 6, 7, 8 ,9]
Need to move a date back in time by a number of days ?
1import { daysBefore } from 'swiss-army-knifey'; 2 3const today = new Date(); // 2022-10-11T03:14:15 4daysBefore(5, today); // 2022-10-6T03:14:15
important: since version 1.17.0
all node depending utilities are exported from file src/node.ts
which possibly means.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
license file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
19 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More