├── .gitignore ├── .npmignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | node_modules 3 | /dist/ 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | /.* 2 | /.*/ 3 | /test/ 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - "8" 5 | - "9" 6 | - "10" 7 | env: 8 | - TEST_SUITE=unit 9 | matrix: 10 | include: 11 | - node_js: "10" 12 | env: TEST_SUITE=standard 13 | script: "npm run-script $TEST_SUITE" 14 | 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Daniel Cousens 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # blacklist 2 | 3 | [![Build Status](https://travis-ci.org/dcousens/blacklist.svg)](https://travis-ci.org/dcousens/blacklist) 4 | [![Version](http://img.shields.io/npm/v/blacklist.svg)](https://www.npmjs.org/package/blacklist) 5 | 6 | This module shallow copies an object, ignoring keys depending on the filter object passed to it. 7 | 8 | Filters can be provided as an object (truthy keys are blacklisted) or string arguments. 9 | 10 | ## ESNext alternative 11 | 12 | If you can, **don't use this library.** 13 | Use ESNext [destructuring assignment](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) instead. 14 | 15 | ``` javascript 16 | let { a, ...filtered } = { a: 1, b: 2, c: 3 } 17 | // filtered => { b: 2, c: 3 } 18 | ``` 19 | 20 | 21 | ### Example 22 | ``` js 23 | var blacklist = require('blacklist') 24 | 25 | // ... 26 | 27 | var someInput = { a: 1, b: 2, c: 3 } 28 | 29 | blacklist(someInput, 'a') 30 | // => { b: 2, c: 3 } 31 | ``` 32 | 33 | **Protip:** you can also use a filter object 34 | ``` js 35 | blacklist(someInput, { 36 | a: true, // a will not be in the result 37 | b: false, // b will be in the result 38 | c: 1 > 2 // false, therefore c will be in the result 39 | }) 40 | // => { b: 2, c: 3 } 41 | ``` 42 | 43 | 44 | ## LICENSE [MIT](LICENSE) 45 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = function blacklist (src) { 2 | var copy = {} 3 | var filter = arguments[1] 4 | 5 | if (typeof filter === 'string') { 6 | filter = {} 7 | for (var i = 1; i < arguments.length; i++) { 8 | filter[arguments[i]] = true 9 | } 10 | } 11 | 12 | for (var key in src) { 13 | // blacklist? 14 | if (filter[key]) continue 15 | 16 | copy[key] = src[key] 17 | } 18 | 19 | return copy 20 | } 21 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "blacklist", 3 | "version": "1.1.4", 4 | "description": "Returns a shallow copy of an object without blacklisted properties", 5 | "main": "index.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/dcousens/blacklist.git" 9 | }, 10 | "homepage": "https://github.com/dcousens/blacklist", 11 | "bugs": { 12 | "url": "https://github.com/dcousens/blacklist/issues" 13 | }, 14 | "scripts": { 15 | "prebuild": "mkdir -p dist", 16 | "build": "umd --commonJS blacklist index.js dist/blacklist.js", 17 | "postbuild": "uglifyjs dist/blacklist.js > dist/blacklist.min.js", 18 | "prepublish": "npm run build", 19 | "standard": "standard", 20 | "unit": "tape test/*.js", 21 | "test": "npm run standard && npm run unit" 22 | }, 23 | "keywords": [ 24 | "blacklist", 25 | "filter", 26 | "object", 27 | "utility" 28 | ], 29 | "author": "Daniel Cousens", 30 | "license": "MIT", 31 | "devDependencies": { 32 | "standard": "*", 33 | "tape": "^4.6.0", 34 | "uglify-js": "^2.6.1", 35 | "umd": "^3.0.1" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var blacklist = require('../') 2 | var tape = require('tape') 3 | 4 | tape('omits properties from a filter object', function (t) { 5 | var someInput = { a: 1, b: 2, c: 3 } 6 | var result = blacklist(someInput, { 7 | a: true, 8 | b: false, 9 | c: false 10 | }) 11 | 12 | t.deepEqual(result, { b: 2, c: 3 }) 13 | t.end() 14 | }) 15 | 16 | tape('omits properties from an arguments list', function (t) { 17 | var someInput = { a: 1, b: 2, c: 3 } 18 | var result = blacklist(someInput, 'b', 'c') 19 | 20 | t.deepEqual(result, { a: 1 }) 21 | t.end() 22 | }) 23 | --------------------------------------------------------------------------------