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

char-series

v2.0.0

Published

Just a simple function to generate an array of characters form the given range string.

Downloads

8

Readme

Char-[ 's', 'e', 'r', 'i', 'e', 's' ]

Just a simple function to generate an array of characters form the given range string. For example "0..9" to generate ["0", "1", "2", ..., "9"]

Importing

The library usages UMD module system so you can use it with any module system of your choice. You can also use it in your html with the <script> tag.

JavaScript

import series from "char-series"; // es module
// or
const series = require("char-series"); // commonjs
// or any other module system that the umd module system supports.

html

<script src="https://unpkg.com/char-series"></script>
<-- It will be available globally by the name "char_series" -->

Usages

The series function takes 4 kinds of argument: string, object, tagged-template, or an array of objects.

series `1..3` // tagged template
series("1..3") // string
series({from: "1", to: "3"}) // object
// all the above function call generates the array ["1", "2", "3"]

series([{from: "1", to: "3"}, {from: "a", to: "c"}]) // object array
// this gives us the array: ["1", "2", "3", "a", "b", "c"]

Note: In html the function name is char_series. So use char_series("1..3").

Argument Types

String Range Syntax

  1. "<from>..<to>" gives us all characters from the from(inclusive) character to the to(inclusive) character. e.g., "a..e", "0..9". We can even get a reversed array just by reversing the start and end order. So "c..a" would give us ["c", "b", "a"]
  2. <before-count>-<char> gives all the characters starting from before-count characters before the char character and ending at the char character. e.g., "2-c" gives us ["a", "b", "c"]
  3. <char>+<after-count> gives all the characters starting from the char character and ending at after-count characters after the char character. e.g., "a+2" gives us ["a", "b", "c"]
  4. <before-count>-<char>+<after-count> is the combination of the previous two method. e.g., "2-c+2" gives us ["a", "b", "c", "d", "e"]

Flag: Any series can be reversed by adding a "!r" flag at the end or switching the from to characters order. e.g., "a..c!r" gives us ["c", "b", "a"] and "c..a!r" gives us ["a", "b", "c"].

Series Concatenation for string argument

Two or more series can be concatenated with an "&" character e.g., "0..9&a..z". But this feature gives rise to a problem though! What if we want to see the series "&..'"? It should return us ["&", "'"] but it throws an exception.

So to solve this problem we've to escape the "&" character with a "/" (Notice: It's not a backslash "\") whenever it doesn't represent a delimiter. And then we also have to escape the "/" with another "/" character if we want to write a single "/" character (Ugh :unamused:)!

Examples: Un-Escaped and Incorrect | Escaped and Correct -------------------------|--------------------- "&..." | "/&..." "*../" | "*..//" "a..&&1..5" | "a../&&1..5" "a..&&1..5&a..c" | "a../&&1..5&a..c!r"

Object Argument Type

  1. {from: char; to: char} e.g., {from: "a", to: "c"}
  2. {char: char; before: number} e.g., {char: "c", before: 2}
  3. {char: char; after: number} e.g., {char: "a", after: 2}
  4. {char: char; before: number; after: number} e.g., {char: "b", before: 1, after: 1}

All the examples above gives us the array ["a", "b", "c"].

Flag: All these objects take an optional {reverse: boolean} property to reverse a series. e.g., {from: "a", to: "c", reverse: true} gives us ["c", "b", "a"] and {from: "c", to: "a", reverse: true} gives us ["a", "b", "c"].

Series Concatenation for object argument

To concatenate tow or more series we can pass an array of series objects, e.g., [{from: "1", to: "3"}, {char: "a", after: 2}] to get the series ["1", "2", "3", "a", "b", "c"].

Examples

const alphanumeric = [...series `0..9`, ...series `a..z`];
// or better if we use series concatenation
const alphanumeric = series `0..9&a..z` // or series("0..9&a..z");
// or 
const alphanumeric = series([{from: "0", to: "9"}, {from: "a", to: "z"}])
/* alphanumeric = [
  '0', '1', '2', '3', '4', '5', '6',
  '7', '8', '9', 'a', 'b', 'c', 'd',
  'e', 'f', 'g', 'h', 'i', 'j', 'k',
  'l', 'm', 'n', 'o', 'p', 'q', 'r',
  's', 't', 'u', 'v', 'w', 'x', 'y',
  'z'
] */

// seems like it works with other languages too :)
const digits_in_bengali = series `০+9` // give us 9 chars after "০"(inclusive)
/* [
  '০', '১', '২', '৩',
  '৪', '৫', '৬', '৭',
  '৮', '৯'
] */

// I wonder what characters are before "0"
const five_chars_before_zero = series({char: "0", before: 5}) // or series `5-0`
/* [ '+', ',', '-', '.', '/', '0' ] */

// 5 characters before "t" and 2 characters after "t" then reverse it
const my_chars = series `5-t+2!r` // or series("5-t+2!r")
//or series({char: "t", before: 5, after: 2, reverse: true})
/* [ 'v', 'u', 't', 's', 'r', 'q', 'p', 'o' ] */

CLI

Yes it also has a cli! Incase you find it useful. But right now it's pretty basic.

# first install it globally or use npx if you prefer
> npm i -g char-series
# see the help text
> series --help

How it works?

Good question! It's really simple :)

function getSeries({ from, to }: {form: number; to: number}) {
  const length = Math.abs(from - to) + 1;

  return from > to
    ? Array.from({ length }, () => String.fromCharCode(from--))
    : Array.from({ length }, () => String.fromCharCode(from++));
}

getSeries({from: "a".charCodeAt(0), to: "c".charCodeAt(0)})
// and we get ["a", "b", "c"]

Finally, let me know if there are any improvements that I can make to this package and kindly give this package a :star: on github if you like it :)