Git Product home page Git Product logo

std's Introduction

My personal JavaScript/TypeScript standard library

Build Status codecov

After some time playing around with Haskell, I miss some of its straightforward functions in the default JavaScript std library. So I'm translating a small portion of Prelude into TypeScript.

Methods

Array.chain

Works similar to Promise.then, but is synchronous.

[1, 2, 3].chain(xs => [...xs, 4]); // => [1, 2, 3, 4]

Array.head

Returns the head of an array.

[1, 2, 3].head(); // => [1]

Array.last

Returns the last element of an array.

[1, 2, 3].last(); // => [3]

Array.product

Returns the product of all the number inside an array of numbers.

[1, 2, 3, 4].product(); // => 12

Array.range

Returns an array of numbers from start to end (inclusive).
You can also specify a step size.

[].range(1, 5); // => [1, 2, 3, 4, 5]
[].range(2, 10, 2); // => [2, 4, 6, 8, 10]
[].range(1.5, 5.5, 0.5); // => [1.5, 2.5, 3.5, 4.5, 5.5]

Array.sum

Returns the sum of all the number inside an array of numbers.

[1, 2, 3, 4].product(); // => 10

Array.tail

Returns the tail of an array.

[1, 2, 3].tail(); // => [2, 3]

Array.transpose

Transposes a matrix.

[[1, 2, 3], [4, 5, 6], [7, 8, 9]].transpose(); // => [[1, 4, 7], [2, 5, 8], [3, 6, 9]]

Array.union

Returns the union of two arrays, with unique values.

[1, 2, 3, 4].union([2, 5, 6]); // => [1, 2, 3, 4, 5, 6]

String.lines

Returns the lines in a string

const str = `Hello!
I am a multiline
string!
`;

str.lines(); // => ['Hello!', 'I am a multiline', 'string!']

String.toList

Converts a string into a list of characters.

"Hello, world!".toList(); // => ['H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!']

String.words

Returns the words in a string.

"Hello, world!".words(); // => ["Hello,", "world!"]

License

MIT

std's People

Contributors

micheleriva avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.