├── .editorconfig ├── .gitattributes ├── .gitignore ├── .npmrc ├── .travis.yml ├── index.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.js text eol=lf 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '8' 4 | - '6' 5 | - '4' 6 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | module.exports = (input, i, count, insert) => { 3 | if (!Buffer.isBuffer(input)) { 4 | throw new TypeError(`Expected a \`Buffer\`, got \`${typeof input}\``); 5 | } 6 | 7 | const index = i > input.length ? input.length : i; 8 | const cnt = count > input.length ? input.length : count; 9 | const insertBuf = Buffer.from(insert); 10 | const start = input.slice(0, i); 11 | const end = input.slice(i + cnt); 12 | const len = index + insertBuf.length + end.length; 13 | 14 | return Buffer.concat([start, insertBuf, end], len); 15 | }; 16 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Kevin Mårtensson (github.com/kevva) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "splice-buffer", 3 | "version": "1.0.0", 4 | "description": "Remove or replace part of a Buffer like Array#splice", 5 | "license": "MIT", 6 | "repository": "kevva/splice-buffer", 7 | "author": { 8 | "name": "Kevin Mårtensson", 9 | "email": "kevinmartensson@gmail.com", 10 | "url": "github.com/kevva" 11 | }, 12 | "engines": { 13 | "node": ">=4" 14 | }, 15 | "scripts": { 16 | "test": "xo && ava" 17 | }, 18 | "files": [ 19 | "index.js" 20 | ], 21 | "keywords": [ 22 | "add", 23 | "buf", 24 | "buffer", 25 | "del", 26 | "delete", 27 | "insert", 28 | "modify", 29 | "mutate", 30 | "remove", 31 | "replace", 32 | "rm", 33 | "splice", 34 | "split" 35 | ], 36 | "devDependencies": { 37 | "ava": "*", 38 | "xo": "*" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # splice-buffer [![Build Status](https://travis-ci.org/kevva/splice-buffer.svg?branch=master)](https://travis-ci.org/kevva/splice-buffer) 2 | 3 | > Remove or replace part of a `Buffer` like `Array#splice` 4 | 5 | 6 | ## Install 7 | 8 | ``` 9 | $ npm install splice-buffer 10 | ``` 11 | 12 | 13 | ## Usage 14 | 15 | ```js 16 | const spliceBuffer = require('splice-buffer'); 17 | 18 | spliceBuffer('unicorn', 3, 4, 'verse'); 19 | //=> 'universe' 20 | ``` 21 | 22 | 23 | ## API 24 | 25 | ### spliceBuffer(input, index, count, [insert]) 26 | 27 | #### input 28 | 29 | Type: `Buffer` 30 | 31 | #### index 32 | 33 | Type: `number` 34 | 35 | Index to start splicing. 36 | 37 | #### count 38 | 39 | Type: `number` 40 | 41 | Number of characters to remove. 42 | 43 | #### insert 44 | 45 | Type: `string` 46 | 47 | String to insert in place of the removed `Buffer`. 48 | 49 | 50 | ## Related 51 | 52 | * [replace-buffer](https://github.com/kevva/replace-buffer) - Replace matches in a `Buffer` 53 | 54 | 55 | ## License 56 | 57 | MIT © [Kevin Mårtensson](http://github.com/kevva) 58 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import m from '.'; 3 | 4 | const buf = (input, ...args) => m(Buffer.from(input), ...args).toString(); 5 | 6 | test('main', t => { 7 | t.is(buf('foobar', 3, 3, 'foo'), 'foofoo'); 8 | t.is(buf('foobar', 5, 3, '1'), 'fooba1'); 9 | t.is(buf('unicorn', 3, 4, 'verse'), 'universe'); 10 | t.is(buf('unicorn', 3, 4, 'verse'), 'universe'); 11 | t.is(buf('example', 0, 0, 'a'), 'aexample'); 12 | t.is(buf('exam\nple', null, null, 'a'), 'aexam\nple'); 13 | t.is(buf('example', 2, 0, 'a'), 'exaample'); 14 | t.is(buf('\u0024example', 3, 1, ''), '$exmple'); 15 | t.is(buf('foobar', 0, 999, 'unicorn'), 'unicorn'); 16 | t.is(buf('foobar', 999, 1, 'foo'), 'foobarfoo'); 17 | }); 18 | 19 | test('throws on wrong input', t => { 20 | t.throws(m.bind(null, 'foo'), 'Expected a `Buffer`, got `string`'); 21 | }); 22 | --------------------------------------------------------------------------------