Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

chars.js

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chars.js

A tiny Javascript single-ascii-character utility/library

  • 0.1.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-66.67%
Maintainers
1
Weekly downloads
 
Created
Source

chars.js


All about single ascii characters. Experimental and under development, will evolve over time.

I use Chars mainly as a document for static character related data and methods. Parts of it can be found in my other libraries, like strings.js for example.



You can use npm install chars.js if you're on node.js.


API

ASCII_RANGE is an array with two indices: [bottomOfRange, topOfRange]. These ranges are directly related to the ordinal values from the ascii-table.

Chars.ASCII_RANGE_UPPERCASE
Chars.ASCII_RANGE_LOWERCASE
Chars.ASCII_RANGE_NUMBERS
Chars.ASCII_RANGE_SPECIAL_1
Chars.ASCII_RANGE_SPECIAL_2
Chars.ASCII_RANGE_SPECIAL_3
Chars.ASCII_RANGE_SPECIAL_4
Chars.ASCII_RANGE_ALL

Chars.REGEXP_SPECIAL_CHARS

<array> REGEXP_SPECIAL_CHARS

Returns an array (like a set) with all special characters used in regular expressions.

I use this for one of my other libraries, strings.js, for escaping a string to prepare for making a new RegExp.

Chars.ascii

<string> Chars.ascii( <string>/<number> ordinal )

Returns the ascii character found at ordinal.

var space= Chars.ascii( 32 );				// ' '

Chars.ordinal

<number> Chars.ordinal( <string>/<number> char )

Returns the ordinal for char.

var ordSpace= Chars.ordinal( ' ' );			// 32

Chars.isUpper

<boolean> Chars.isUpper( <string>/<number> char )

Returns true if char is uppercase.

var test= Chars.isUpper( 'z' );				// false

Chars.isLower

<boolean> Chars.isLower( <string>/<number> char )

Returns true if char is lowercase.

var test= Chars.isLower( 'z' );				// true

Chars.isAlpha

<boolean> Chars.isAlpha( <string>/<number> char )

Returns true if char is uppercase or lowercase alpha.

var test= Chars.isAlpha( 'a' );				// true

Chars.isNumeric

<boolean> Chars.isNumeric( <string>/<number> char )

Returns true if char is a number.

var test= Chars.isNumeric( '0' );			// true
var test= Chars.isNumeric( 0 );				// true

Chars.isSpecial

<boolean> Chars.isSpecial( <string>/<number> char )

Returns true if char is a special character from the SPECIAL_1,2,3 or 4 range.

var test= Chars.isSpecial( '.' );			// true

Chars.isAlphaNumeric

<boolean> Chars.isAlphaNumeric( <string>/<number> char )

Returns true if char is a uppercase, lowercase or number.

var test= Chars.isAlphaNumeric( 'A' );		// true
var test= Chars.isAlphaNumeric( 1 );		// true

Chars.random

<string> Chars.random( range )

Returns a random character in range range. range defaults to the Chars.ASCII_RANGE_ALL range

var test= Chars.random( Chars.ASCII_RANGE_UPPERCASE );
// returns a single random character between 'A' and 'Z'

Chars.prototype.constructor

<this> constructor( <string>/<number> char, <array> range )

char can be a number character or ordinal, range has the format of ASCII_RANGE

var char= new Chars( '?' );
// I will be using this char instance for the following examples.

Chars.prototype.get

<string> get()

Returns this.char

console.log( char.get() );					// '?'

Chars.prototype.set

<string> set( <string>/<number> char )

char can be a number character or ordinal. If char is an ordinal, the character represented by ordinal will be set.

console.log( char.set('!') );				// '!'

Chars.prototype.next

<string> next( <string>/<number> amount )

Proceed this.char to the following ascii character and returns it, if no arguments are given. Proceed to amount ascii characters ahead, limited by this.range, if amount is set.

console.log( char.next() );					//	'@'

Chars.prototype.prev

<this> prev( <string>/<number> amount )

Sets and returns this.char to the predecessor of the current ascii character in the ascii-table, if no arguments are given. If amount is given, it will decrease and set to the amount characters back in the ascii table.

prev is limited to the current active range. So, casting a prev on the bounds of the range will keep returning this bound.

console.log( char.prev() );					//	'!'

The remaining methods are similar in working as the static versions by the same name, they only do not require a string argument, they use the objects string instead.

Chars.prototype.isUpper

<boolean> isUpper()

Chars.prototype.isLower

<boolean> isLower()

Chars.prototype.isAlpha

<boolean> isAlpha()

Chars.prototype.isNumeric

<boolean> isNumeric()

Chars.prototype.isSpecial

<boolean> isSpecial()

Chars.prototype.isAlphaNumeric

<boolean> isAlphaNumeric()

Chars.prototype.random

<string> random()

Keywords

FAQs

Package last updated on 16 Oct 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc