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

char-buffer

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

char-buffer - npm Package Compare versions

Comparing version 0.3.3 to 0.4.0

71

char-buffer.js

@@ -38,17 +38,80 @@ 'use strict';

/**
* @chainable
* @abstract
*
* Write a charCode to the buffer at an offset.
*
* @param {Number} charCode The charCode to write.
* @param {Number} offset The zero based offset to write at.
* @throws {Error} if offset < 0 or offset > this.length
*/
CharBuffer.prototype.write = undefined;
/**
* @abstract
*
* Read the charCode at an offset.
*
* @param {Number} offset The zero based offset.
* @return {Number} The charCode.
* @throws {Error} if offset < 0 or offset >= this.length
*/
CharBuffer.prototype.read = undefined;
/**
* @abstract
*
* Read the charCode at an offset.
*
* @param {Number} offset The zero based offset.
* @return {Number} The charCode.
* @throws {Error} if offset < 0 or offset >= this.length
*/
CharBuffer.prototype.charCodeAt = undefined;
/**
* @abstract
*
* Read the char at an offset.
*
* @param {Number} offset The zero based offset.
* @return {String} The char.
* @throws {Error} if offset < 0 or offset >= this.length
*/
CharBuffer.prototype.charAt = undefined;
/**
* @property {Number} length Length of the {@link String} represented by this buffer.
* @readonly
*/
CharBuffer.prototype.length = 0;
/**
* @abstract
*
* Gets the length of the {@link String} represented by this buffer.
* @return {Number} The length of the string.
* @return {Number} The length of the {@link String}.
*/
CharBuffer.prototype.getLength = undefined;
CharBuffer.prototype.getLength = function(){
return this.length;
};
/**
* @chainable
* @abstract
*
* Sets the length of the {@link String} represented by this buffer.
* @param {Number} newLength The new length.
* @throws {RangeError} if `newLength < 0 || newLength > this.length`
*/
CharBuffer.prototype.setLength = undefined;
CharBuffer.prototype.setLength = function(newLength){
var msg;
if(newLength < 0 || newLength > this.length){
msg = 'newLength must be between 0 and ' + (this.length);
msg += ', ' + newLength + ' given.';
throw new RangeError(msg);
}
this.length = newLength;
return this;
};

@@ -55,0 +118,0 @@ /**

40

node-buffer.js

@@ -25,5 +25,5 @@ 'use strict';

}
CharBuffer.call(this);
initCapacity = initCapacity || 16;
this._buffer = new Buffer(initCapacity*2);
this._length = 0;
}

@@ -59,3 +59,3 @@

/**
* Appends a charCode to the buffer using
* Write a charCode to the buffer using
* [Buffer.writeUInt16LE(charCode, ...)][1].

@@ -65,7 +65,11 @@ *

* @param {Number} charCode The charCode to append.
* @param {Number} offset The zero based offset to write at.
*/
NodeBuffer.prototype.append = function(charCode){
this._ensureCapacity(this._length+1);
this._buffer.writeUInt16LE(charCode, this._length*2);
this._length++;
NodeBuffer.prototype.write = function(charCode, offset){
if(typeof offset === 'undefined'){
offset = this.length;
}
this._ensureCapacity(offset+1);
this._buffer.writeUInt16LE(charCode, offset*2);
this.length = offset+1 > this.length ? offset+1 : this.length, true;
return this;

@@ -75,19 +79,17 @@ };

/** */
NodeBuffer.prototype.setLength = function(newLength){
var msg;
if(newLength < 0 || newLength*2 > this._buffer.length){
msg = 'newLength must be between 0 and ' + (this._buffer.length/2);
msg += ', ' + newLength + ' given.';
throw new RangeError(msg);
}
this._length = newLength;
return this;
NodeBuffer.prototype.append = NodeBuffer.prototype.write;
/** */
NodeBuffer.prototype.read = function(offset){
return this._buffer.readUInt16LE(offset*2);
};
/** */
NodeBuffer.prototype.getLength = function(){
return this._length;
NodeBuffer.prototype.charCodeAt = NodeBuffer.prototype.read;
/** */
NodeBuffer.prototype.charAt = function(offset){
return String.fromCharCode(this.read(offset));
};
/**

@@ -102,3 +104,3 @@ * Returns the {@link String} represented by this buffer using

NodeBuffer.prototype.toString = function(){
return this._buffer.toString('utf16le', 0, this._length*2);
return this._buffer.toString('utf16le', 0, this.length*2);
};

@@ -105,0 +107,0 @@

{
"name": "char-buffer",
"version": "0.3.3",
"version": "0.4.0",
"description": "Collect CharCodes and convert them to string.",

@@ -5,0 +5,0 @@ "homepage": "https://github.com/schnittstabil/char-buffer",

@@ -49,3 +49,3 @@ # char-buffer [![Dependencies Status Image](https://gemnasium.com/schnittstabil/char-buffer.svg)](https://gemnasium.com/schnittstabil/char-buffer) [![Build Status Image](https://travis-ci.org/schnittstabil/char-buffer.svg)](https://travis-ci.org/schnittstabil/char-buffer) [![Coverage Status](https://coveralls.io/repos/schnittstabil/char-buffer/badge.png)](https://coveralls.io/r/schnittstabil/char-buffer) [![Built with Grunt](https://cdn.gruntjs.com/builtwith.png)](http://gruntjs.com/)

// Same as before, but provide an estimate for the length of your string:
// Same as before, but provide an estimate of the length of your string:
buffer = new CharBuffer(3);

@@ -59,6 +59,6 @@

// Append a Charcode:
// Append a CharCode:
buffer.append(102);
// Append two more Charcodes:
// Append two more CharCodes:
buffer.append(111).append(111);

@@ -101,10 +101,9 @@

```html
<!-- load CharBuffer -->
<script src="bower_components/char-buffer/char-buffer.js"></script>
<script src="bower_components/char-buffer/char-buffer.global.js"></script>
<script>
// create the default CharBuffer implementation:
// Create the default CharBuffer implementation:
var buffer = new CharBuffer(3);
// or create a specific CharBuffer implementation:
// Or create a specific CharBuffer implementation:
var TypedArrayBuffer = CharBuffer.TypedArrayBuffer,

@@ -114,3 +113,3 @@ buffer = new TypedArrayBuffer(3);

// output 'foo'
// Output 'foo'
console.log(buffer.append(102).append(111).append(111).toString());

@@ -124,8 +123,6 @@

See [Asynchronous Module Definition (AMD)](https://github.com/amdjs/amdjs-api/blob/master/AMD.md) for Details.
See [Asynchronous Module Definition (AMD)](https://github.com/amdjs/amdjs-api/blob/master/AMD.md) for details.
```html
<!-- (optinal) load your amd loader: -->
<script src="path/to/your/amd/loader.js"></script>
<!-- load CharBuffer -->
<script src="bower_components/char-buffer/char-buffer.js"></script>

@@ -135,3 +132,3 @@ <script>

/**
* use the CharBuffer package:
* Use the CharBuffer package:
*/

@@ -142,3 +139,3 @@ require(['char-buffer'], function(CharBuffer){

// or create a specific CharBuffer implementation by CharBuffer:
// Or create a specific CharBuffer implementation by CharBuffer:
var TypedArrayBuffer = CharBuffer.TypedArrayBuffer,

@@ -148,3 +145,3 @@ buffer = new TypedArrayBuffer(3);

// output 'foo'
// Output 'foo'
console.log(buffer.append(102).append(111).append(111).toString());

@@ -155,3 +152,3 @@ });

/**
* or use a specific CharBuffer package:
* Or use a specific CharBuffer package:
*/

@@ -162,3 +159,3 @@ require(['char-buffer/typed-array-buffer'], function(TypedArrayBuffer){

// output 'foo'
// Output 'foo'
console.log(buffer.append(102).append(111).append(111).toString());

@@ -165,0 +162,0 @@ });

@@ -24,5 +24,5 @@ 'use strict';

}
CharBuffer.call(this);
initCapacity = initCapacity || 16;
this._buffer = new Array(initCapacity);
this._length = 0;
}

@@ -38,9 +38,14 @@

/**
* Appends a charCode to the buffer using
* Write a charCode to the buffer using
* {@link String#fromCharCode} and {@link Array#push []}.
*
* @param {Number} charCode The charCode to append.
* @param {Number} offset The zero based offset to write at.
*/
StringArrayBuffer.prototype.append = function(charCode){
this._buffer[this._length++] = String.fromCharCode(charCode);
StringArrayBuffer.prototype.write = function(charCode, offset){
if(typeof offset === 'undefined'){
offset = this.length;
}
this._buffer[offset] = String.fromCharCode(charCode);
this.length = offset+1 > this.length ? offset+1 : this.length, true;
return this;

@@ -50,18 +55,17 @@ };

/** */
StringArrayBuffer.prototype.setLength = function(newLength){
var msg;
if(newLength < 0 || newLength > this._buffer.length){
msg = 'newLength must be between 0 and ' + (this._buffer.length);
msg += ', ' + newLength + ' given.';
throw new RangeError(msg);
}
this._length = newLength;
return this;
StringArrayBuffer.prototype.append = StringArrayBuffer.prototype.write;
/** */
StringArrayBuffer.prototype.read = function(offset){
return this._buffer[offset].charCodeAt(0);
};
/** */
StringArrayBuffer.prototype.getLength = function(){
return this._length;
StringArrayBuffer.prototype.charAt = function(offset){
return this._buffer[offset];
};
/** */
StringArrayBuffer.prototype.charCodeAt = StringArrayBuffer.prototype.read;
/**

@@ -72,3 +76,3 @@ * Returns the {@link String} represented by this buffer.

StringArrayBuffer.prototype.toString = function(){
return this._buffer.slice(0,this._length).join('');
return this._buffer.slice(0,this.length).join('');
};

@@ -75,0 +79,0 @@

@@ -14,3 +14,3 @@ 'use strict';

*
* Constructs a StringArrayBuffer representing an empty string.
* Constructs a StringBuffer representing an empty string.
*/

@@ -21,2 +21,3 @@ function StringBuffer(){

}
CharBuffer.call(this);
this._buffer = '';

@@ -33,9 +34,23 @@ }

/**
* Appends a charCode to the buffer using
* Write a charCode to the buffer using
* {@link String#fromCharCode} and {@link String#concat +}.
*
* @param {Number} charCode The charCode to append.
* @param {Number} offset The zero based offset to write at.
*/
StringBuffer.prototype.write = function(charCode, offset){
if(typeof offset === 'undefined' || offset === this.length){
return this.append(charCode);
}
var pre = this._buffer.slice(0, offset),
post = this._buffer.slice(offset+1);
this._buffer = pre + String.fromCharCode(charCode) + post;
this.length = this._buffer.length;
return this;
};
/** */
StringBuffer.prototype.append = function(charCode){
this._buffer += String.fromCharCode(charCode);
this.length = this._buffer.length;
return this;

@@ -45,18 +60,21 @@ };

/** */
StringBuffer.prototype.setLength = function(newLength){
var msg;
if(newLength < 0 || newLength > this._buffer.length){
msg = 'newLength must be between 0 and ' + (this._buffer.length);
msg += ', ' + newLength + ' given.';
throw new RangeError(msg);
}
this._buffer = this._buffer.slice(0, newLength);
return this;
StringBuffer.prototype.charCodeAt = function(offset){
return this._buffer.charCodeAt(offset);
};
/** */
StringBuffer.prototype.getLength = function(){
return this._buffer.length;
StringBuffer.prototype.charAt = function(offset){
return this._buffer.charAt(offset);
};
/** */
StringBuffer.prototype.read = StringBuffer.prototype.charCodeAt;
/** */
StringBuffer.prototype.setLength = function(newLength){
this.constructor.prototype.setLength.call(this, newLength);
this._buffer = this._buffer.slice(0, this.length);
return this;
};
/**

@@ -63,0 +81,0 @@ * Returns the internal {@link String}.

@@ -27,4 +27,7 @@ 'use strict';

function describeBasicTest(BufferConstr, data, useNew){
var MAX_LEN = 40,
dataLen = data.length,
var dataLen = data.length,
MAX_LEN = dataLen<=40 ? dataLen : 40,
MID = Math.round(dataLen/2),
TEST_CHAR = 'a',
TEST_CHARCODE = TEST_CHAR.charCodeAt(0),
shortened = data.substring(0, MAX_LEN),

@@ -43,4 +46,4 @@ bufferConstr = BufferConstr;

buffer.append(data.charCodeAt(j));
if(j === Math.round(dataLen/2)){
expect(buffer.getLength()).to.be(j+1);
if(j === MID){
expect(buffer.length).to.be(j+1);
expect(buffer.toString()).to.be(data.substr(0,j+1));

@@ -50,4 +53,17 @@ }

expect(buffer.length).to.be(dataLen);
expect(buffer.toString()).to.be(data);
buffer.write(TEST_CHARCODE, MID);
for(j=0; j<MAX_LEN; j++){
if(j===MID){
expect(buffer.charAt(j)).to.be(TEST_CHAR);
expect(buffer.charCodeAt(j)).to.be(TEST_CHARCODE);
}else{
expect(buffer.charAt(j)).to.be(data.charAt(j));
expect(buffer.charCodeAt(j)).to.be(data.charCodeAt(j));
}
}
expect(function(){

@@ -63,2 +79,8 @@ buffer.setLength(-1);

expect(function(){
// same as append
buffer.write(data.charCodeAt(1), buffer.getLength());
}).not.to.throwException();
expect(buffer.toString()).to.be(data.substr(0,2));
done();

@@ -65,0 +87,0 @@ });

@@ -27,5 +27,5 @@ 'use strict';

}
CharBuffer.call(this);
initCapacity = initCapacity || 16;
this._buffer = new Uint16Array(initCapacity);
this._length = 0;
}

@@ -64,6 +64,11 @@

* @param {Number} charCode The charCode to append.
* @param {Number} offset The zero based offset to write at.
*/
TypedArrayBuffer.prototype.append = function(charCode){
this._ensureCapacity(this._length+1);
this._buffer[this._length++] = charCode;
TypedArrayBuffer.prototype.write = function(charCode, offset){
if(typeof offset === 'undefined'){
offset = this.length;
}
this._ensureCapacity(offset+1);
this._buffer[offset] = charCode;
this.length = offset+1 > this.length ? offset+1 : this.length, true;
return this;

@@ -73,16 +78,15 @@ };

/** */
TypedArrayBuffer.prototype.setLength = function(newLength){
var msg;
if(newLength < 0 || newLength > this._buffer.length){
msg = 'newLength must be between 0 and ' + (this._buffer.length);
msg += ', ' + newLength + ' given.';
throw new RangeError(msg);
}
this._length = newLength;
return this;
TypedArrayBuffer.prototype.append = TypedArrayBuffer.prototype.write;
/** */
TypedArrayBuffer.prototype.read = function(offset){
return this._buffer[offset];
};
/** */
TypedArrayBuffer.prototype.getLength = function(){
return this._length;
TypedArrayBuffer.prototype.charCodeAt = TypedArrayBuffer.prototype.read;
/** */
TypedArrayBuffer.prototype.charAt = function(offset){
return String.fromCharCode(this.read(offset));
};

@@ -108,3 +112,3 @@

var ARGS_MAX = 65535,
len = this._length,
len = this.length,
buf = '',

@@ -111,0 +115,0 @@ startPos = 0,

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