├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | 3 | language: node_js 4 | 5 | node_js: 6 | - "5" 7 | - "4" 8 | - "0.12" 9 | - "0.10" 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # to-buffer 2 | 3 | Pass in a string, get a buffer back. Pass in a buffer, get the same buffer back. 4 | 5 | ``` 6 | npm install to-buffer 7 | ``` 8 | 9 | [![build status](https://travis-ci.org/mafintosh/to-buffer.svg?branch=master)](https://travis-ci.org/mafintosh/to-buffer) 10 | 11 | ## Usage 12 | 13 | ``` js 14 | var toBuffer = require('to-buffer') 15 | console.log(toBuffer('hi')) // 16 | console.log(toBuffer(Buffer('hi'))) // 17 | console.log(toBuffer('6869', 'hex')) // 18 | console.log(toBuffer(43)) // throws 19 | ``` 20 | 21 | ## License 22 | 23 | MIT 24 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = toBuffer 2 | 3 | var makeBuffer = Buffer.from && Buffer.from !== Uint8Array.from ? Buffer.from : bufferFrom 4 | 5 | function bufferFrom (buf, enc) { 6 | return new Buffer(buf, enc) 7 | } 8 | 9 | function toBuffer (buf, enc) { 10 | if (Buffer.isBuffer(buf)) return buf 11 | if (typeof buf === 'string') return makeBuffer(buf, enc) 12 | if (Array.isArray(buf)) return makeBuffer(buf) 13 | throw new Error('Input should be a buffer or a string') 14 | } 15 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "to-buffer", 3 | "version": "1.1.1", 4 | "description": "Pass in a string, get a buffer back. Pass in a buffer, get the same buffer back", 5 | "main": "index.js", 6 | "dependencies": {}, 7 | "devDependencies": { 8 | "standard": "^6.0.5", 9 | "tape": "^4.4.0" 10 | }, 11 | "scripts": { 12 | "test": "standard && tape test.js" 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "https://github.com/mafintosh/to-buffer.git" 17 | }, 18 | "author": "Mathias Buus (@mafintosh)", 19 | "license": "MIT", 20 | "bugs": { 21 | "url": "https://github.com/mafintosh/to-buffer/issues" 22 | }, 23 | "homepage": "https://github.com/mafintosh/to-buffer" 24 | } 25 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var tape = require('tape') 2 | var toBuffer = require('./') 3 | 4 | tape('buffer returns buffer', function (t) { 5 | t.same(toBuffer(Buffer('hi')), Buffer('hi')) 6 | t.end() 7 | }) 8 | 9 | tape('string returns buffer', function (t) { 10 | t.same(toBuffer('hi'), Buffer('hi')) 11 | t.end() 12 | }) 13 | 14 | tape('string + enc returns buffer', function (t) { 15 | t.same(toBuffer('6869', 'hex'), Buffer('hi')) 16 | t.end() 17 | }) 18 | 19 | tape('other input throws', function (t) { 20 | try { 21 | toBuffer(42) 22 | } catch (err) { 23 | t.same(err.message, 'Input should be a buffer or a string') 24 | t.end() 25 | } 26 | }) 27 | --------------------------------------------------------------------------------