npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ayc0/std

v0.0.12

Published

Standard JS functions

Downloads

17

Readme

Standard

Import

To install, run either npm install --save @ayc0/std or yarn add @ayc0/std, and then:

const std = require('@ayc0/std');
const multiplyBy2 = std.map(x => x * 2);

// OR

const map = require('@ayc0/std/map');
const multiplyBy2 = map(x => x * 2);

// And then you can use it:

const input = [1, 2];
const output = multiplyBy2(input);

Supported functions

For all of the functions listed below, you can either do std.<function> or import them from @ayc0/std/<function>:

Range

function* range([from,] to[, step]) {
  // yield every steps
}

Zip

function* zip(iterables) {
  // yield [currentValues, indexes, iterables]
}

Supported iterables

  • array
  • string
  • Map
  • Set
  • Object
  • iterators

Examples

for (const [currentValues, indexes, iterables] of zip([
  '123',
  [1, 2, 3],
  { a: 1, b: 2, c: 3 },
])) {
  console.log(currentValues);
  // [ '1', 1, 1 ]
  // [ '2', 2, 2 ]
  // [ '3', 3, 3 ]
  console.log(indexes);
  // [ 0, 0, 'a' ]
  // [ 1, 1, 'b' ]
  // [ 2, 2, 'c' ]
}

Len

function len(iterable) {
  // return length of iterable
}

Supported iterables

  • array
  • string
  • Map
  • Set
  • Object
  • iterators

Map

function map(callback[, thisArg]) {
  return function (iterable) {
    // return new iterable of the input iterable type
  }
}

function callback(currentValue[, index[, iterable]]) {
  // return new element of iterable
}

Supported iterables

  • array
  • string
  • Map
  • Set
  • Object
  • iterators

Examples

map(x => x * 2)({ a: 1, b: 2 });
// { a: 2, b: 4 }
map(x => x * 2)([1, 2]);
// [ 2, 4 ]
map(x => x * 2)(new Set([1, 2]));
// Set(2) {2, 4}
map(x => x * 2)(
  new Map([
    ['a', 1],
    ['b', 2],
  ]),
);
// Map(2) {"a" => 2, "b" => 4}
map(x => x.repeat(2))('ab');
// 'aabb'

ForEach

function forEach(callback[, thisArg]) {
  return function (iterable) {
  }
}

function callback(currentValue[, index[, iterable]]) {
}

Supported iterables

  • array
  • string
  • Map
  • Set
  • Object
  • iterators

Examples

forEach(x => console.log(x * 2))({ a: 1, b: 2 });
// 2
// 4
forEach(x => console.log(x * 2))([1, 2]);
// 2
// 4
forEach(x => console.log(x * 2))(new Set([1, 2]));
// 2
// 4
forEach(x => console.log(x * 2))(
  new Map([
    ['a', 1],
    ['b', 2],
  ]),
);
// 2
// 4
forEach(x => console.log(x.repeat(2)))('ab');
// aa
// bb

Filter

function filter(callback[, thisArg]) {
  return function (iterable) {
    // return new iterable of the input iterable type
  }
}

function callback(currentValue[, index[, iterable]]) {
  // return if you should keep this element of not
}

Supported iterables

  • array
  • string
  • Map
  • Set
  • Object
  • iterators

Examples

filter(x => x % 2)({ a: 1, b: 2 });
// { a: 1 }
filter(x => x % 2)([1, 2]);
// [ 1 ]
filter(x => x % 2)(new Set([1, 2]));
// Set(1) {1}
filter(x => x % 2)(
  new Map([
    ['a', 1],
    ['b', 2],
  ]),
);
// Map(1) {"a" => 1}
filter(x => x % 2)('12');
// '1'

Reduce

function reduce(callback, initialValue, thisArg) {
  return function (iterable) {
    // return reduced value
  };
}

function callback(accumulator, currentValue[, index[, iterable]]) {
  // returns the value that results from the reduction
}

Supported iterables

  • array
  • string
  • Map
  • Set
  • Object
  • iterators

Examples

reduce((acc, x) => acc + x, 0)({ a: 1, b: 2 });
// { a: 1 }
reduce((acc, x) => acc + x, 0)([1, 2]);
// [ 1 ]
reduce((acc, x) => acc + x, 0)(new Set([1, 2]));
// Set(1) {1}
reduce(
  (acc, x) => acc + x,
  0,
)(
  new Map([
    ['a', 1],
    ['b', 2],
  ]),
);
// Map(1) {"a" => 1}
reduce((acc, x) => acc + Number(x), 0)('12');
// '1'

Take

function take(limit) {
  return function (iterable) {
    // return new iterable with only the <limit> first items
  };
}

Supported iterables

  • array
  • string
  • Map
  • Set
  • Object
  • iterators

Examples

take(1)({ a: 1, b: 2 });
// { a: 1 }
take(1)([1, 2]);
// [ 1 ]
take(1)(new Set([1, 2]));
// Set(1) {1}
take(1)(
  new Map([
    ['a', 1],
    ['b', 2],
  ]),
);
// Map(1) {"a" => 1}
take(1)('ab');
// 'a'

Drop

function drop(limit) {
  return function (iterable) {
    // return new iterable with <limit> first item trimmed
  };
}

Supported iterables

  • array
  • string
  • Map
  • Set
  • Object
  • iterators

Examples

drop(1)({ a: 1, b: 2 });
// { b: 2 }
drop(1)([1, 2]);
// [ 2 ]
drop(1)(new Set([1, 2]));
// Set(1) {2}
drop(1)(
  new Map([
    ['a', 1],
    ['b', 2],
  ]),
);
// Map(1) {"b" => 2}
drop(1)('ab');
// 'b'

Find

function find(callback[, thisArg]) {
  return function (iterable) {
    // return the element and its key or undefined
  }
}

function callback(currentValue[, index[, iterable]]) {
  // return if you it matches your element or not
}

Supported iterables

  • array
  • string
  • Map
  • Set
  • Object
  • iterators

Examples

find(x => x % 2)({ a: 1, b: 2 });
// [ 1, 'a' ]
find(x => x % 2)([1, 2]);
// [ 1, 0 ]
find(x => x % 2)(new Set([1, 2]));
// [ 1, 0 ]
find(x => x % 2)(
  new Map([
    ['a', 1],
    ['b', 2],
  ]),
);
// [ 1, 'a' ]
find(x => x % 2)('12');
// [ '1', 0 ]

Every

function every(callback[, thisArg]) {
  return function (iterable) {
    // return true if all elements match the callback
  }
}

function callback(currentValue[, index[, iterable]]) {
  // return if you it matches your element or not
}

Supported iterables

  • array
  • string
  • Map
  • Set
  • Object
  • iterators

Examples

every(x => x <= 2)({ a: 1, b: 2 });
// true
every(x => x <= 2)([1, 2]);
// true
every(x => x <= 2)(new Set([1, 2]));
// true
every(x => x <= 2)(
  new Map([
    ['a', 1],
    ['b', 2],
  ]),
);
// true
every(x => x === '1')('12');
// false

Some

function some(callback[, thisArg]) {
  return function (iterable) {
    // return true if at least 1 element matches the callback
  }
}

function callback(currentValue[, index[, iterable]]) {
  // return if you it matches your element or not
}

Supported iterables

  • array
  • string
  • Map
  • Set
  • Object
  • iterators

Examples

some(x => x <= 2)({ a: 1, b: 2 });
// true
some(x => x <= 2)([1, 2]);
// true
some(x => x <= 2)(new Set([1, 2]));
// true
some(x => x <= 2)(
  new Map([
    ['a', 1],
    ['b', 2],
  ]),
);
// true
some(x => x === '1')('12');
// true

Iterator

Iterator allows you to chain operations more easily. It supports all these methods:

  • drop
  • take
  • map
  • filter
  • every
  • some
  • reduce
  • find
  • len
  • forEach

You can also use the method build() to reconstruct an iterable (either from the same type as the input, or you can transform it).

Supported iterables

  • array
  • string
  • Map
  • Set
  • Object
  • iterators

Examples

const iterable = Iterable.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
iterable
  .drop(1)
  .filter(x => x % 2 === 0)
  .map(x => x * 3)
  .take(2)
  .build();
// [6, 12]

build() accepts an optional parameter type:

const iterable = Iterable.from([1, 2, 3]);
iterable.build(type.Set);
// Set([1, 2, 3])