diff --git a/Readme.md b/Readme.md index 271a567..7f391c2 100644 --- a/Readme.md +++ b/Readme.md @@ -1,6 +1,6 @@ # buffer-compare-polyfill -Polyfills Node v0.10.x with v0.12.x `Buffer.prototype.compare` functionality. +Polyfills Node v0.10.x with v0.12.x `Buffer.compare` and `Buffer.prototype.compare` functionality. See [v.12 API Docs](http://nodejs.org/api/buffer.html#buffer_buf_compare_otherbuffer) @@ -8,4 +8,8 @@ See [v.12 API Docs](http://nodejs.org/api/buffer.html#buffer_buf_compare_otherbu Buffer('123').compare(Buffer('123')); // 0 Buffer('123').compare(Buffer('1230')); // -1 Buffer('1230').compare(Buffer('123')); // 1 + + Buffer.compare(Buffer('123'), Buffer('123')); // 0 + Buffer.compare(Buffer('123'), Buffer('1230')); // -1 + Buffer.compare(Buffer('1230'), Buffer('123')); // 1 ``` diff --git a/index.js b/index.js index 45646e8..32f4401 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,9 @@ var compare = require('buffer-compare'); +Buffer.compare = Buffer.compare || function (buffer1, buffer2) { + return compare(buffer1, buffer2); +}; + Buffer.prototype.compare = Buffer.prototype.compare || function (buffer) { return compare(this, buffer); -} +}; diff --git a/test.js b/test.js index b3cfc21..5d822d7 100644 --- a/test.js +++ b/test.js @@ -17,3 +17,18 @@ console.log('Passed'); console.log('should return -1 when there is no match'); console.assert(A.compare(C) === -1, 'Failed:' + A.compare(C)); console.log('Passed'); + +console.log('Buffer.compare : '); + +console.log('should return 0 when a byte sequences matches and buffers are of equal length'); +console.assert(Buffer.compare(A, A) === 0, 'Failed:' + Buffer.compare(A, A)); +console.log('Passed'); +console.log('should return 1 when a byte sequence matches and the left buffer is smaller'); +console.assert(Buffer.compare(A, B) === 1, 'Failed:' + Buffer.compare(A, B)); +console.log('Passed'); +console.log('should return 1 when a byte sequence matches and the left buffer is larger'); +console.assert(Buffer.compare(B, A) === -1, 'Failed:' + Buffer.compare(B, A)); +console.log('Passed'); +console.log('should return -1 when there is no match'); +console.assert(Buffer.compare(A, C) === -1, 'Failed:' + Buffer.compare(A, C)); +console.log('Passed');