1.31.0 • Published 2 months ago

swiss-army-knifey v1.31.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

Swiss Army Knifey

Tools you could have gotten somewhere else, but you still decided to go this way.

Ramda Compose With Promise

Added typings to composeWithPromise

  it('should await and return a composed result', async function() {
    const paramToProp = (param: string) => ({ param });
    const sleepingSentence = (s: number) => async ({ param }: { param: string }) => {
      const d1 = new Date();
      await sleep(s, TimeUnit.second);
      const d2 = new Date();
      return `a_[${param}], slept for: ${d2.getTime() - d1.getTime()}`;
    };
    const toUp = async (param: string) => param.toUpperCase();
    const removeStart = async (param: string) => param.slice(1);
    const composed = composeWithPromise(
      removeStart,
      sleepingSentence(1),
      paramToProp,
      toUp,
      sleepingSentence(2),
      paramToProp
    );
    return expect(composed('hey')).to.eventually.match(/_\[A_\[HEY\], SLEPT FOR: \d{4}\], slept for: \d{3,4}$/g);
  });

Sleep

Example 0

Very simple sleep:

import { sleep, TimeUnits } from 'swiss-army-kinfey';

(async function main() {

while (1) {
  await sleep(1000);
  console.log('Sleeping forever, in 1000 milliseconds invervals')
}

while (1) {
  await sleep(1, TimeUnit.hours);
  console.log('Sleeping forever, in 1 hour invervals')
}

})();

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

Example 1

import { sleepForCallback, TimeUnit } from 'swiss-army-knifey';

(async function main() {

  await sleepForCallback(TimeUnit.seconds)(6, (current: number, total: number, unit: TimeUnit) => {
    console.log(`Sleeping for ${total} ${unit}, but calling this on every step: ${current}${unit.substring(0,1)}`);
  });

})();

Output :

Sleeping for 6 seconds, but calling this on every step: 0s
Sleeping for 6 seconds, but calling this on every step: 1s
Sleeping for 6 seconds, but calling this on every step: 2s
Sleeping for 6 seconds, but calling this on every step: 3s
Sleeping for 6 seconds, but calling this on every step: 4s
Sleeping for 6 seconds, but calling this on every step: 5s

Example 2

Or equivalently :

import { sleepSecondsCallback, TimeUnit } from 'swiss-army-knifey';

(async function main() {

  await sleepSecondsCallback(6, (current: number, total: number, unit: TimeUnit) => {
    console.log(`Sleeping for ${total} ${unit}, but calling this on every step: ${current}${unit.substring(0,1)}`);
  });

})();

Produces the same output as above

Array

Ever wondered if you could get rid of imperative for/while loops using arrays ?

import { getArrayRange } from 'swiss-army-knifey';
getArrayRange(-4, 5); // [-4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
getArrayRange(0, 9); // [0, 1, 2, 3, 4, 5, 6, 7, 8 ,9]

Days Before

Need to move a date back in time by a number of days ?

import { daysBefore } from 'swiss-army-knifey';

const today = new Date(); // 2022-10-11T03:14:15
daysBefore(5, today); // 2022-10-6T03:14:15

Change log

important: since version 1.17.0 all node depending utilities are exported from file src/node.ts which possibly means.

1.31.0

2 months ago

1.30.0

2 months ago

1.29.0

2 months ago

1.29.1

2 months ago

1.28.0

6 months ago

1.27.2

12 months ago

1.27.3

12 months ago

1.27.1

12 months ago

1.21.0

1 year ago

1.21.1

1 year ago

1.25.0

1 year ago

1.23.0

1 year ago

1.21.2

1 year ago

1.27.0

1 year ago

1.23.4

1 year ago

1.20.1

1 year ago

1.22.0

1 year ago

1.20.0

1 year ago

1.26.0

1 year ago

1.24.0

1 year ago

1.14.0

1 year ago

1.12.1

1 year ago

1.12.0

1 year ago

1.18.0

1 year ago

1.16.0

1 year ago

0.11.0

1 year ago

0.11.1

1 year ago

0.9.0

1 year ago

0.9.2

1 year ago

0.9.1

1 year ago

1.15.0

1 year ago

1.13.2

1 year ago

1.13.1

1 year ago

1.13.0

1 year ago

1.19.0

1 year ago

1.17.0

1 year ago

0.10.1

1 year ago

0.10.2

1 year ago

0.10.0

1 year ago

0.8.0

1 year ago

0.7.11

2 years ago

0.7.10

2 years ago

0.7.9

2 years ago

0.7.12

2 years ago

0.7.6

2 years ago

0.7.5

2 years ago

0.7.8

2 years ago

0.7.7

2 years ago

0.7.4

2 years ago

0.7.3

2 years ago

0.7.1

2 years ago

0.6.2

2 years ago

0.7.0

2 years ago

0.6.1

2 years ago

0.6.0

2 years ago

0.5.6

2 years ago

0.4.6

2 years ago

0.4.5

2 years ago

0.4.4

2 years ago

0.4.2

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.1

2 years ago