Socket
Socket
Sign inDemoInstall

string-etc

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

string-etc

Collection of supplementary JavaScript String methods that works across module packaging systems and that can be included piece-wise


Version published
Weekly downloads
233
increased by34.68%
Maintainers
1
Weekly downloads
 
Created
Source

Build Status npm version Bower version

A collection of handy String methods that works across the various JavaScript packaging systems, including DOM/Browser, CommonJS/Node.js, and AMD/Require.js. Methods can be included piecewise. E-mail me if you find a String method that you think should be included because it would be useful for other people as well.

Usage

Web page

Accessing these extensions will differ depending on our module system. If we are on Browser or AMD, we can access these methods directly from an String object. For example, we can do:

'California'.cram(8) // returns Califor…

Node wrapper

However, if we are on CommonJS/Node, we can access these methods through a wrapper. So instead of the call above, we would have:

string('California').cram(8) // returns Califor…

where string is a constructed function that wraps around the string on which we want to operate:

var string = require('array-etc').wrap(['cram']);

This extra wrapper is a special arrangement we added on Node, in order to avoid global conflicts with the String.prototype object. We may have other libraries or other versions of this library in our dependency tree. Unbeknownst to us, these libraries may add methods of similar names to the String.prototype object. We can attach the methods locally to a wrapping function to avoid these potential collisions.

Since unintentional collisions are a lot harder with script tag loads or AMD, where the end developer actively controls the loading of the modules, we have not seen a case for extending the wrapping function to these systems.

Node loader

Using a wrapper on Node ensures safety. However, it does introduce a speed bump in that a extra function call must be required before the string can be operated. Calls to library methods with a single source string would require nesting.

If safety is not an issue, you can use the direct syntax in Node as well with a loader call. For example:

require('array-etc').load(['cram']);

This will load cram into String.prototype

Installation

Web page

If you use bower, run

bower install string-etc

If you use npm, run

npm install string-etc

Add the corresponding script tag to your page. e.g.

<script src="/bower_components/string-etc/lib/cram.js"></script>

Node

In your shell, run

npm install string-etc

In your file, require the library, and call wrap with the methods you want your wrapper have. For example:

var string = require('string-etc').wrap(['cram']);

Or if you prefer the direct syntax without wrapping:

require('string-etc').load(['cram']);

Libraries

lib/cram.js

Cram takes a string and shortens it, by replacing

Keywords

FAQs

Package last updated on 26 Jun 2015

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