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

type-reverse

v2.0.2

Published

🦄 Lightweight reverse utility around strings, arrays, numbers and more.

Downloads

41

Readme

type-reverse

Build Status tested with jest Made in Nigeria

🦄 Lightweight reverse utility around strings, arrays, numbers and more.

Install

$ npm install --save type-reverse

Usage

const reverse = require('type-reverse')

or...

import reverse from 'type-reverse'

API

reverse( input[, options][, callback] )

Params

  • input {String|Number|Array|Set}
  • options {?Object}
  • callback {?Function}
  • returns {*}
reverse('pizza')
//=> azzip

Works with numbers too.

reverse(1234)
//=> 4321

Reversing arrays...

When JavaScript's Array#reverse method is used, the original array is mutated, as in, the indexes of the elements are changed. On the other hand, this utility adopts the non-destructive array reversal method, which means the reverse() function doesn't mutate the array; it just returns the reversed array and still maintains the indexes of the elements in the original array.

native reverse...

const arr = [1, 2, 3]
arr.reverse() //=> [3, 2, 1]

Oops, we lost the indexes of elements in the initial array...

console.log(arr) //=> [3, 2, 1]

vs...

🦄 to the rescue...

const arr = [1, 2, 3]
reverse(arr) //=> [3, 2, 1]

Yay! arr is not mutated. The indexes of its elements are still maintained...

console.log(arr) //=> [1, 2, 3]

Sets

If you've been wondering how to reverse Sets in JavaScript, here's it! The core reverse function can take in a Set as the input and then return the reversed Set...

const set = new Set([5, 4, 3, 4, 5])
reverse(set) //=> Set { 3, 4, 5 }

options

options is the second parameter to the function call and it is an object with two available properties. It can also take in a falsy value which would implicity get converted to an empty object.

invert: {String}

This property defaults to index and applies to strings and numbers only.

reverse(/*...*/, {
  invert: '[index|word|sign]'
})
  • index - interchanges the indexes of characters in the input...

    reverse(12345, { invert: 'index' }) //=> 54321
    reverse('of... unicorns', { invert: 'index' }) //=> snrocinu ...fo
  • sign - inverts the sign in a number...

    reverse(1234, { invert: 'sign' }) //=> -1234
  • word - swaps the location of words in a string...

    reverse('of... unicorns', { invert: 'word' }) //=> unicorns of...

preserveZeros: {Boolean}

This property defaults to true. It specifies whether to enforce preceding zeros in the result of a number that contains trailing zeros. See #3 for more info. Note that the result gets converted to a string. Disabling it would look like this...

reverse(240, { preserveZeros: false }) //=> 42

callback: {Function}

The callback takes in a function with two optional parameters that represent input and result respectively.

  • input - the initial input that was passed into the function
  • result - the output from reversing the input
const text = 'dog'

reverse(text, null, function(intitial, result) {
  return intitial + ' was changed to ' + result
}) //=> dog was changed to god

Limits

Did you just try to reverse a reaally huge number? Unfortunately, this utility doesn't support very large numbers. Trying to do so with this utility would throw a TypeError.

Author

Olaolu Olawuyi

License

MIT © Olaolu Olawuyi