char-buffer
Advanced tools
Comparing version 0.5.10 to 0.6.0
@@ -39,6 +39,7 @@ 'use strict'; | ||
/** | ||
* @abstract | ||
* @method | ||
* @chainable | ||
* @abstract | ||
* | ||
* Write a charCode to the buffer at an offset. | ||
* Writes a charCode to the buffer at an offset. | ||
* | ||
@@ -53,4 +54,5 @@ * @param {Number} charCode The charCode to write. | ||
* @abstract | ||
* @method | ||
* | ||
* Read the charCode at an offset. | ||
* Reads the charCode at an offset. | ||
* | ||
@@ -65,4 +67,5 @@ * @param {Number} offset The zero based offset. | ||
* @abstract | ||
* @method | ||
* | ||
* Read the charCode at an offset. | ||
* Reads the charCode at an offset. | ||
* | ||
@@ -77,4 +80,5 @@ * @param {Number} offset The zero based offset. | ||
* @abstract | ||
* @method | ||
* | ||
* Read the char at an offset. | ||
* Reads the char at an offset. | ||
* | ||
@@ -95,2 +99,3 @@ * @param {Number} offset The zero based offset. | ||
* @abstract | ||
* @method | ||
* | ||
@@ -105,2 +110,3 @@ * Gets the length of the {@link String} represented by this buffer. | ||
/** | ||
* @method | ||
* @chainable | ||
@@ -124,3 +130,65 @@ * | ||
/** | ||
* @method | ||
* | ||
* Executes a function once per charCode. | ||
* See also {@link Array#forEach} | ||
* | ||
* @param {Function} callback Function to execute for each charCode. | ||
* @param {Number} callback.charCode The charCode. | ||
* @param {Number} callback.index The index of the charCode. | ||
* @param {Object} callback.charbuffer The CharBuffer being traversed. | ||
* @param {Object} [thisArg=undefined] Value to use as this when executing callback. | ||
*/ | ||
AbstractCharBuffer.prototype.forEach = function(callback, thisArg) { | ||
var T, | ||
i, | ||
len = this.length; | ||
if (typeof callback !== 'function') { | ||
throw new TypeError(callback + ' is not a function'); | ||
} | ||
if (arguments.length > 1) { | ||
T = thisArg; | ||
} | ||
for (i = 0; i < len; i++) { | ||
callback.call(T, this.charCodeAt(i), i, this); | ||
} | ||
}; | ||
/** | ||
* @method | ||
* | ||
* Creates a new CharBuffer with the results of calling a provided function on every charCode. | ||
* See also {@link Array#map} | ||
* | ||
* @param {Function} callback Function to execute for each charCode. | ||
* @param {Number} callback.charCode The charCode. | ||
* @param {Number} callback.index The index of the charCode. | ||
* @param {Object} callback.charbuffer The CharBuffer being traversed. | ||
* @param {Number} callback.return The new charCode to write into the new CharBuffer. | ||
* @param {Object} [thisArg=undefined] Value to use as this when executing callback. | ||
* @return {CharBuffer} CharBuffer of the return values of callback function. | ||
*/ | ||
AbstractCharBuffer.prototype.map = function(callback, thisArg) { | ||
var T, | ||
i, | ||
len = this.length, | ||
output = new this.constructor(len); | ||
if (typeof callback !== 'function') { | ||
throw new TypeError(callback + ' is not a function'); | ||
} | ||
if (arguments.length > 1) { | ||
T = thisArg; | ||
} | ||
for (i = 0; i < len; i++) { | ||
output.append(callback.call(T, this.charCodeAt(i), i, this)); | ||
} | ||
return output; | ||
}; | ||
/** | ||
* @abstract | ||
* @method | ||
* | ||
@@ -133,9 +201,68 @@ * Returns the {@link String} represented by this buffer. | ||
/** | ||
* @property {Boolean} | ||
* @static | ||
* @property {Boolean} [isSupported=true] | ||
* @template | ||
* Indicates whether this AbstractCharBuffer is supported by the current platform. | ||
* @inheritable | ||
* Indicates whether this CharBuffer is supported by the current platform. | ||
*/ | ||
AbstractCharBuffer.isSupported = false; | ||
/** | ||
* @static | ||
* @method | ||
* @inheritable | ||
* | ||
* Creates a new CharBuffer from a {@link String}. | ||
* | ||
* @param {String} string The string. | ||
* @param {Function} [transform=identity] Function that produces a charCode of the new CharBuffer | ||
* from a charCode of the string parameter. | ||
* @param {Number} transform.charCode The charCode of the string. | ||
* @param {Number} transform.index The index of the charCode within the string. | ||
* @param {Number} transform.return The charCode to write into the new CharBuffer. | ||
* @return {CharBuffer} CharBuffer of the string, transformed by transform. | ||
* | ||
* @example | ||
* var charBuffer; | ||
* | ||
* charBuffer = CharBuffer.fromString('abc'); | ||
* console.log(charBuffer.toString()); // output: abc | ||
* | ||
* charBuffer = CharBuffer.fromString('abc', function(charCode, index){ | ||
* return charCode + 3; | ||
* }); | ||
* console.log(charBuffer.toString()); // output: def | ||
* | ||
*/ | ||
AbstractCharBuffer.fromString = null; | ||
/** | ||
* @static | ||
* @method | ||
* @protected | ||
* | ||
* Creates a fromString implementation. | ||
* | ||
* @param {Function} Constr A CharBuffer constructor. | ||
* @return {Function} A default fromString implementation for Constr. | ||
*/ | ||
AbstractCharBuffer.fromStringConstr = function(Constr) { | ||
return function(string, transform) { | ||
var len = string.length, | ||
output = new Constr(len), | ||
i; | ||
// manual loop optimization :-) | ||
if (transform) { | ||
for (i = 0; i < len; i++) { | ||
output.append(transform.call(transform, string.charCodeAt(i), i)); | ||
} | ||
} else { | ||
for (i = 0; i < len; i++) { | ||
output.append(string.charCodeAt(i)); | ||
} | ||
} | ||
return output; | ||
}; | ||
}; | ||
module.exports = AbstractCharBuffer; |
@@ -10,2 +10,3 @@ 'use strict'; | ||
* @class CharBuffer | ||
* @extends CharBuffer.AbstractCharBuffer | ||
*/ | ||
@@ -45,9 +46,8 @@ | ||
} | ||
} | ||
/** | ||
* @static | ||
* @property {String[]} [supported=["StringBuffer", "StringArrayBuffer", | ||
* "TypedArrayBuffer", "NodeBuffer"]] | ||
* @static | ||
* | ||
@@ -60,4 +60,4 @@ * Names of the supported {@link CharBuffer.AbstractCharBuffer} implementations of the | ||
/** | ||
* @static | ||
* @property {CharBuffer[]} CharBuffers | ||
* @static | ||
* | ||
@@ -64,0 +64,0 @@ * Array of all {@link CharBuffer.AbstractCharBuffer} implementations. |
@@ -32,2 +32,4 @@ 'use strict'; | ||
NodeBuffer.prototype.constructor = NodeBuffer; | ||
/* istanbul ignore if: IE-fix */ | ||
@@ -39,3 +41,3 @@ if (!NodeBuffer.name) { | ||
/** | ||
* @method _ensureCapacity | ||
* @method | ||
* @protected | ||
@@ -60,2 +62,3 @@ * | ||
/** | ||
* @method | ||
* Write a charCode to the buffer using | ||
@@ -78,6 +81,6 @@ * [Buffer.writeUInt16LE(charCode, ...)][1]. | ||
/** */ | ||
/** @method */ | ||
NodeBuffer.prototype.append = NodeBuffer.prototype.write; | ||
/** */ | ||
/** @method */ | ||
NodeBuffer.prototype.read = function(offset) { | ||
@@ -87,6 +90,6 @@ return this._buffer.readUInt16LE(offset * 2); | ||
/** */ | ||
/** @method */ | ||
NodeBuffer.prototype.charCodeAt = NodeBuffer.prototype.read; | ||
/** */ | ||
/** @method */ | ||
NodeBuffer.prototype.charAt = function(offset) { | ||
@@ -97,2 +100,3 @@ return String.fromCharCode(this.read(offset)); | ||
/** | ||
* @method | ||
* Returns the {@link String} represented by this buffer using | ||
@@ -109,6 +113,3 @@ * [Buffer.toString('utf16le', ...)][1]. | ||
/** | ||
* @inheritdoc CharBuffer.AbstractCharBuffer#isSupported | ||
* @static | ||
*/ | ||
/** @static @property */ | ||
NodeBuffer.isSupported = (function() { | ||
@@ -124,2 +125,5 @@ try { | ||
/** @static @method */ | ||
NodeBuffer.fromString = AbstractCharBuffer.fromStringConstr(NodeBuffer); | ||
module.exports = NodeBuffer; |
{ | ||
"name": "char-buffer", | ||
"version": "0.5.10", | ||
"version": "0.6.0", | ||
"description": "Collect CharCodes and convert them to string.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/schnittstabil/char-buffer", |
@@ -231,3 +231,19 @@ # 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/) | ||
## Changelog | ||
* v0.6.0 __char-buffer becomes more array-like__ | ||
* New: CharBuffer#map(callback, [thisArg]) | ||
* New: CharBuffer#forEach(callback, [thisArg]) | ||
* __New: CharBuffer#fromString(string, [transform])__ | ||
* Fix: *Buffer.prototype.constructor | ||
* v0.5.0 __[component](https://github.com/component/component) support__ | ||
* v0.4.0 __char-buffer becomes more string-like__ | ||
* New: CharBuffer#charCodeAt(offset): CharCode | ||
* New: CharBuffer#charAt(offset): String | ||
* New: CharBuffer#length: Number | ||
* New: CharBuffer#read(offset): CharCode | ||
* New: CharBuffer#write(charCode, offset) | ||
## License | ||
@@ -234,0 +250,0 @@ |
@@ -31,2 +31,4 @@ 'use strict'; | ||
StringArrayBuffer.prototype.constructor = StringArrayBuffer; | ||
/* istanbul ignore if: IE-fix */ | ||
@@ -38,2 +40,3 @@ if (!StringArrayBuffer.name) { | ||
/** | ||
* @method | ||
* Write a charCode to the buffer using | ||
@@ -54,6 +57,6 @@ * {@link String#fromCharCode} and {@link Array#push []}. | ||
/** */ | ||
/** @method */ | ||
StringArrayBuffer.prototype.append = StringArrayBuffer.prototype.write; | ||
/** */ | ||
/** @method */ | ||
StringArrayBuffer.prototype.read = function(offset) { | ||
@@ -63,3 +66,3 @@ return this._buffer[offset].charCodeAt(0); | ||
/** */ | ||
/** @method */ | ||
StringArrayBuffer.prototype.charAt = function(offset) { | ||
@@ -69,6 +72,7 @@ return this._buffer[offset]; | ||
/** */ | ||
/** @method */ | ||
StringArrayBuffer.prototype.charCodeAt = StringArrayBuffer.prototype.read; | ||
/** | ||
* @method | ||
* Returns the {@link String} represented by this buffer. | ||
@@ -81,8 +85,8 @@ * @return {String} The string. | ||
/** | ||
* @inheritdoc CharBuffer.AbstractCharBuffer#isSupported | ||
* @static | ||
*/ | ||
/** @static @property */ | ||
StringArrayBuffer.isSupported = true; | ||
/** @static @method */ | ||
StringArrayBuffer.fromString = AbstractCharBuffer.fromStringConstr(StringArrayBuffer); | ||
module.exports = StringArrayBuffer; |
@@ -26,2 +26,4 @@ 'use strict'; | ||
StringBuffer.prototype.constructor = StringBuffer; | ||
/* istanbul ignore if: IE-fix */ | ||
@@ -33,2 +35,3 @@ if (!StringBuffer.name) { | ||
/** | ||
* @method | ||
* Write a charCode to the buffer using | ||
@@ -51,3 +54,3 @@ * {@link String#fromCharCode} and {@link String#concat +}. | ||
/** */ | ||
/** @method */ | ||
StringBuffer.prototype.append = function(charCode) { | ||
@@ -59,3 +62,3 @@ this._buffer += String.fromCharCode(charCode); | ||
/** */ | ||
/** @method */ | ||
StringBuffer.prototype.charCodeAt = function(offset) { | ||
@@ -65,3 +68,3 @@ return this._buffer.charCodeAt(offset); | ||
/** */ | ||
/** @method */ | ||
StringBuffer.prototype.charAt = function(offset) { | ||
@@ -71,8 +74,8 @@ return this._buffer.charAt(offset); | ||
/** */ | ||
/** @method */ | ||
StringBuffer.prototype.read = StringBuffer.prototype.charCodeAt; | ||
/** */ | ||
/** @method */ | ||
StringBuffer.prototype.setLength = function(newLength) { | ||
this.constructor.prototype.setLength.call(this, newLength); | ||
AbstractCharBuffer.prototype.setLength.call(this, newLength); | ||
this._buffer = this._buffer.slice(0, this.length); | ||
@@ -83,2 +86,3 @@ return this; | ||
/** | ||
* @method | ||
* Returns the internal {@link String}. | ||
@@ -91,8 +95,27 @@ * @return {String} The string. | ||
/** | ||
* @inheritdoc CharBuffer.AbstractCharBuffer#isSupported | ||
* @static | ||
*/ | ||
/** @static @property */ | ||
StringBuffer.isSupported = true; | ||
/** @static @method */ | ||
StringBuffer.fromString = function(string, transform) { | ||
var output = new StringBuffer(), | ||
len = string.length, | ||
buffer, | ||
i; | ||
if (transform) { | ||
buffer = ''; | ||
for (i = 0; i < len; i++) { | ||
buffer += String.fromCharCode(transform.call(transform, string.charCodeAt(i), i)); | ||
} | ||
} else { | ||
// JavaScript strings are immutable | ||
buffer = string; | ||
} | ||
output._buffer = buffer; | ||
output.length = len; | ||
return output; | ||
}; | ||
module.exports = StringBuffer; |
'use strict'; | ||
var CharBuffer = require('../char-buffer'); | ||
var expect = require('expect'); | ||
var forEach = require('./for-each'); | ||
function buildTestString(len) { | ||
var strBuffer = '', | ||
i, | ||
charCode; | ||
var testStrings = require('./test-strings'); | ||
for (i = 0; i < len; i++) { | ||
charCode = i % 94 + 32; // only ASCII CharCodes | ||
strBuffer += String.fromCharCode(charCode); | ||
function shortenString(string, maxLength) { | ||
var len = string.length <= maxLength ? string.length : maxLength, | ||
shortened = string.substring(0, len); | ||
if (len < string.length) { | ||
shortened += '...'; | ||
} | ||
return strBuffer; | ||
return shortened; | ||
} | ||
var testStrings = { | ||
defaults: [ | ||
'user@example.com', | ||
'\uD834\uDF06', | ||
'latinкирилицаαβγδεζηあいうえお', | ||
buildTestString(500), | ||
buildTestString(65535 * 2 + 1) | ||
] | ||
}, | ||
i, charBufferName; | ||
function describeBasicTest(BufferConstr, testString, useNew) { | ||
function describeBasicTest(BufferConstr, testString) { | ||
var testStringLen = testString.length, | ||
@@ -34,4 +23,3 @@ MAX_LEN = testStringLen <= 40 ? testStringLen : 40, | ||
TEST_CHARCODE = TEST_CHAR.charCodeAt(0), | ||
shortened = testString.substring(0, MAX_LEN), | ||
bufferConstr = BufferConstr; | ||
shortened = shortenString(testString, MAX_LEN); | ||
@@ -43,3 +31,3 @@ if (testStringLen > MAX_LEN) { | ||
it('should work well on "' + shortened + '"', function(done) { | ||
var buffer = useNew ? new BufferConstr(testStringLen) : bufferConstr(testStringLen), | ||
var buffer = new BufferConstr(testStringLen), | ||
j; | ||
@@ -97,12 +85,2 @@ | ||
function describeBasicTests(BufferConstr, dataArray) { | ||
dataArray = dataArray || testStrings.defaults; | ||
describe(BufferConstr.name, function() { | ||
for (var i = 0; i < dataArray.length; i++) { | ||
describeBasicTest(BufferConstr, dataArray[i], i % 2); | ||
} | ||
}); | ||
} | ||
function describeAppendFunction(SUT) { | ||
@@ -150,6 +128,18 @@ if (SUT.isSupported) { | ||
for (i = 0; i < CharBuffer.supported.length; i++) { | ||
charBufferName = CharBuffer.supported[i]; | ||
describeBasicTests(CharBuffer[charBufferName]); | ||
} | ||
forEach(CharBuffer.supported, function(charBufferName) { | ||
var Constr = CharBuffer[charBufferName]; | ||
describe(Constr.name, function() { | ||
switch (Constr.name) { | ||
case 'TypedArrayBuffer': | ||
forEach(testStrings.slow, function(testString) { | ||
describeBasicTest(Constr, testString); | ||
}); | ||
break; | ||
default: | ||
forEach(testStrings.fast, function(testString) { | ||
describeBasicTest(Constr, testString); | ||
}); | ||
} | ||
}); | ||
}); | ||
@@ -166,6 +156,95 @@ describe('default CharBuffer', function() { | ||
for (i = 0; i < CharBuffer.CharBuffers.length; i++) { | ||
describeShouldBeAnAbstractCharBufferInstance(CharBuffer.CharBuffers[i]); | ||
describeAppendFunction(CharBuffer.CharBuffers[i]); | ||
} | ||
forEach(CharBuffer.CharBuffers, function(Constr) { | ||
describeShouldBeAnAbstractCharBufferInstance(Constr); | ||
describeAppendFunction(Constr); | ||
}); | ||
}); | ||
function plusOne(x) { | ||
return x + 1; | ||
} | ||
function minusOne(x) { | ||
return x - 1; | ||
} | ||
forEach(CharBuffer.supported, function(charBufferName) { | ||
describe(charBufferName + '.fromString', function() { | ||
forEach(testStrings.fast, function(string) { | ||
it('(minusOne ° plusOne) should act like identity for ' + shortenString(string, 40), function() { | ||
expect(CharBuffer[charBufferName].fromString(string).toString()).to.be(string); | ||
var p1 = CharBuffer[charBufferName].fromString(string, plusOne).toString(), | ||
id = CharBuffer[charBufferName].fromString(p1, minusOne).toString(); | ||
expect(id).to.be(string); | ||
}); | ||
}); | ||
}); | ||
}); | ||
forEach(CharBuffer.supported, function(charBufferName) { | ||
describe(charBufferName + '.map', function() { | ||
it('throws exception on non callback', function() { | ||
expect(function() { | ||
new CharBuffer[charBufferName]().map(null); | ||
}).to.throwException(/not a function/); | ||
}); | ||
it('should respect thisArg', function() { | ||
var thisArg = { count: 0 }, | ||
buffer = new CharBuffer[charBufferName](3); | ||
buffer.append(102).append(111); | ||
buffer.map(function(charCode, index, charBuffer) { | ||
expect(charBuffer).to.be(buffer); | ||
expect(charCode).to.be(index ? 111 : 102); | ||
expect(this).to.be(thisArg); | ||
this.count++; | ||
return charCode; | ||
}, thisArg); | ||
expect(thisArg.count).to.be(2); | ||
}); | ||
forEach(testStrings.fast, function(string) { | ||
it('(minusOne ° plusOne) should act like identity for ' + shortenString(string, 40), function() { | ||
var org = CharBuffer[charBufferName].fromString(string), | ||
p1 = org.map(plusOne), | ||
id = p1.map(minusOne); | ||
expect(id.toString()).to.be(string); | ||
}); | ||
}); | ||
}); | ||
}); | ||
forEach(CharBuffer.supported, function(charBufferName) { | ||
describe(charBufferName + '.forEach', function() { | ||
it('throws exception on non callback', function() { | ||
expect(function() { | ||
new CharBuffer[charBufferName]().forEach(null); | ||
}).to.throwException(/not a function/); | ||
}); | ||
it('should call callback with every written charCode', function() { | ||
var count = 0, | ||
buffer = new CharBuffer[charBufferName](3); | ||
buffer.append(102).append(111); | ||
buffer.forEach(function(charCode, index, charBuffer) { | ||
expect(charBuffer).to.be(buffer); | ||
expect(charCode).to.be(index ? 111 : 102); | ||
count++; | ||
}); | ||
expect(count).to.be(2); | ||
}); | ||
it('should respect thisArg', function() { | ||
var thisArg = { count: 0 }, | ||
buffer = new CharBuffer[charBufferName](2); | ||
buffer.append(102).append(111); | ||
buffer.forEach(function() { | ||
expect(this).to.be(thisArg); | ||
this.count++; | ||
}, thisArg); | ||
expect(thisArg.count).to.be(2); | ||
}); | ||
}); | ||
}); |
@@ -34,2 +34,4 @@ 'use strict'; | ||
TypedArrayBuffer.prototype.constructor = TypedArrayBuffer; | ||
/* istanbul ignore if: IE-fix */ | ||
@@ -41,3 +43,3 @@ if (!TypedArrayBuffer.name) { | ||
/** | ||
* @method _ensureCapacity | ||
* @method | ||
* @protected | ||
@@ -62,2 +64,3 @@ * | ||
/** | ||
* @method | ||
* Appends a charCode to the buffer using [...]. | ||
@@ -78,6 +81,6 @@ * | ||
/** */ | ||
/** @method */ | ||
TypedArrayBuffer.prototype.append = TypedArrayBuffer.prototype.write; | ||
/** */ | ||
/** @method */ | ||
TypedArrayBuffer.prototype.read = function(offset) { | ||
@@ -87,6 +90,6 @@ return this._buffer[offset]; | ||
/** */ | ||
/** @method */ | ||
TypedArrayBuffer.prototype.charCodeAt = TypedArrayBuffer.prototype.read; | ||
/** */ | ||
/** @method */ | ||
TypedArrayBuffer.prototype.charAt = function(offset) { | ||
@@ -98,2 +101,3 @@ return String.fromCharCode(this.read(offset)); | ||
/** | ||
* @method | ||
* Returns the {@link String} represented by this buffer using | ||
@@ -142,6 +146,3 @@ * {@link String#fromCharCode}. | ||
/** | ||
* @inheritdoc CharBuffer.AbstractCharBuffer#isSupported | ||
* @static | ||
*/ | ||
/** @static @property */ | ||
TypedArrayBuffer.isSupported = (function() { | ||
@@ -156,2 +157,5 @@ try { | ||
/** @static @method */ | ||
TypedArrayBuffer.fromString = AbstractCharBuffer.fromStringConstr(TypedArrayBuffer); | ||
module.exports = TypedArrayBuffer; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
120961
13
1311
254