Socket
Socket
Sign inDemoInstall

string-etc

Package Overview
Dependencies
0
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.3.0 to 0.4.0

2

bower.json
{
"name": "string-etc",
"version": "0.3.0",
"version": "0.4.0",
"homepage": "https://github.com/dicksont/string-etc",

@@ -5,0 +5,0 @@ "authors": [

@@ -29,3 +29,3 @@ /*

var chunk = function(width) {
var chunk = function(str, width) {
var arr = [];

@@ -36,4 +36,4 @@

for (var i=0; i < this.length; i+=width) {
arr.push(this.substr(i, width));
for (var i=0; i < str.length; i+=width) {
arr.push(str.substr(i, width));
}

@@ -47,5 +47,7 @@

} else { // Browser
String.prototype.chunk = chunk;
String.prototype.chunk = function() {
return chunk(this,width);
}
}
})();

@@ -29,8 +29,6 @@ /*

var cram = function(space, opts) {
if (space == null) {
space = 16;
}
var cram = function(str, space, opts) {
space = (space != null)? space : 16;
if (this.length <= space) return this;
if (str.length <= space) return str;

@@ -50,3 +48,3 @@ opts = opts || {}

if (opts.location == 'head') {
return opts.replacement + this.substr(-1 * (space - ellipsisLength),space - ellipsisLength);
return opts.replacement + str.substr(-1 * (space - ellipsisLength),space - ellipsisLength);
} else if (opts.location == 'body') {

@@ -56,5 +54,5 @@ var prelength = Math.ceil((space - ellipsisLength)/2);

return this.substr(0, prelength) + opts.replacement + this.substr(-1 * postlength, postlength);
return str.substr(0, prelength) + opts.replacement + str.substr(-1 * postlength, postlength);
} else if (opts.location == 'tail') {
return this.substr(0, space - ellipsisLength) + opts.replacement;
return str.substr(0, space - ellipsisLength) + opts.replacement;
} else {

@@ -68,5 +66,7 @@ throw new Error("String.prototype.cram: Invalid value" + opts.location + " for option ellipsisPosition. It can only have the following values: ('head', 'body', 'tail') ");

} else { // Browser
String.prototype.cram = cram;
String.prototype.cram = function(space, opts) {
return cram(this, space, opts);
}
}
})();

@@ -29,3 +29,3 @@ /*

var pad = function(width, opts) {
var pad = function(str, width, opts) {

@@ -38,11 +38,11 @@ opts = opts || {}

if (this.length > 0) {
if (str.length > 0) {
if (opts.location == 'tail') {
return this + pad.call('', width - this.length, opts);
return str + pad('', width - str.length, opts);
} else if (opts.location == 'head') {
return pad.call('', width - this.length, opts) + this;
return pad('', width - str.length, opts) + str;
}
}
if (width < 0) return this;
if (width < 0) return str;

@@ -61,5 +61,7 @@ var padding = "";

} else { // Browser
String.prototype.pad = pad;
String.prototype.pad = function(width, opts) {
return pad(this, width, opts);
}
}
})();

@@ -65,3 +65,3 @@ /*

for(fx in methods) {
Wrapper[fx] = methods[fx].bind(str);
Wrapper[fx] = methods[fx].bind(undefined, str);
}

@@ -68,0 +68,0 @@

{
"name": "string-etc",
"version": "0.3.0",
"version": "0.4.0",
"description": "Collection of supplementary JavaScript String methods that works across module packaging systems and that can be included piece-wise ",

@@ -5,0 +5,0 @@ "main": "node/etc.js",

@@ -5,10 +5,15 @@ [![Build Status](https://travis-ci.org/dicksont/string-etc.svg?branch=master)](https://travis-ci.org/dicksont/string-etc)

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.
A collection of handy String methods that works across various JavaScript module systems, including:
- Browser script tag
- CommonJS / Node.js
- AMD / Require.js.
Methods can be included piecewise.
## Usage
Accessing these extensions will differ depending on our module system.
### Web page
If we are on *Browser* or *AMD*, we can access these methods directly from an *String* object. For example, we can do:
### HTML web page
If we are on a vanilla web page or AMD, we can access these methods directly from an *String* object. For example, we can do:

@@ -21,4 +26,6 @@ ```javascript

However, if we are on *CommonJS/Node*, we can access these methods through a wrapper. So instead of the call above, we would have:
However, this type of direct access may lead to conflicts on CommonJS systems like Node. Unlike AMD or web pages, libraries are automatically loaded in Node. We may have other libraries in our dependency tree that add similarly named methods to the same *String.prototype* object. When we call these methods, we may be in fact be calling the function that overwrote ours.
We added a special arrangement on Node to prevent this problem. We create a local wrapper function. Instead of attaching the methods to the global *String.prototype* object, we attach them to this function. We would have:
```javascript

@@ -28,3 +35,3 @@ string('California').cram(8) // returns Califor…

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

@@ -35,12 +42,12 @@ ```javascript

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 these kind of unintended 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 extended the wrapper function to these systems.
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.
The wrapper function on Node guarantees safety. However, it may be harder to manipulate. We need an extra function call for every string operation, and string operations cannot be chained as elegantly.
If safety is not an issue, you can use the direct syntax in Node as well with a loader call. For example:
If safety is not an issue, we can use the direct syntax in Node as well. We just need to call *load* instead of *wrap* in our setup.
For example:
```javascript

@@ -50,7 +57,7 @@ require('array-etc').load(['cram']);

This will load **cram** into **String.prototype**
will load **cram** into **String.prototype**
## Installation
### Web page
If you use bower, run
If we use Bower, we would run:
```

@@ -60,3 +67,3 @@ bower install string-etc

If you use npm, run
If we use npm, we would run:
```

@@ -66,3 +73,3 @@ npm install string-etc

Add the corresponding script tag to your page. e.g.
Then, we add the corresponding script tag to our page. e.g.

@@ -74,3 +81,3 @@ ```html

### Node
In your shell, run
In our shell, run:
```shell

@@ -80,3 +87,5 @@ npm install string-etc

In your file, require the library, and call wrap with the methods you want your wrapper have. For example:
In our file, require *string-etc*.
Call wrap with the methods we desire. For example:
```javascript

@@ -86,3 +95,3 @@ var string = require('string-etc').wrap(['cram']);

Or if you prefer the direct syntax without wrapping:
Or call load if we prefer the direct syntax without wrapping:
```javascript

@@ -92,4 +101,31 @@ require('string-etc').load(['cram']);

## Libraries
# Libraries
### lib/cram.js
Cram takes a string and shortens it, by replacing
## String.prototype.cram(space, opts)
Cram tries to fit a string within a given width, by replacing excess characters with an ellipsis:
For example, suppose we are building a web page that indexes different Objective-C methods. Objective-C has some pretty long function names that might mess up our layout. Using cram, we can shorten these. Let's try a pretty bad case:
```javascript
"splitViewController:willHideViewController:withBarButtonItem:forPopoverController:".cram(); //"splitViewContro…"
```
By default, cram returns a string with max length 16. However, we can adjust this by passing in a number:
```javascript
"splitViewController:willHideViewController:withBarButtonItem:forPopoverController:".cram(8); //"splitVi…"
```
### opts.replacement
The replacement character by default is an ellipsis, but you can use another string. Specify the replacement string, and cram will adjust accordingly.
## Technical Support
E-mail me:
- if you have problems or questions.
- if you find a String method that you think should be included, because it would be useful for others as well.
- if you have an interesting use case that I should consider
SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc