├── .editorconfig ├── .eslintrc.json ├── .gitattributes ├── .gitignore ├── .npmrc ├── .travis.yml ├── .verb.md ├── CHANGELOG.md ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org/ 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | end_of_line = lf 7 | indent_size = 2 8 | indent_style = space 9 | insert_final_newline = true 10 | trim_trailing_whitespace = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "eslint:recommended" 4 | ], 5 | 6 | "env": { 7 | "browser": false, 8 | "es6": true, 9 | "node": true, 10 | "mocha": true 11 | }, 12 | 13 | "parserOptions":{ 14 | "ecmaVersion": 9, 15 | "sourceType": "module", 16 | "ecmaFeatures": { 17 | "modules": true, 18 | "experimentalObjectRestSpread": true 19 | } 20 | }, 21 | 22 | "globals": { 23 | "document": false, 24 | "navigator": false, 25 | "window": false 26 | }, 27 | 28 | "rules": { 29 | "accessor-pairs": 2, 30 | "arrow-spacing": [2, { "before": true, "after": true }], 31 | "block-spacing": [2, "always"], 32 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 33 | "comma-dangle": [2, "never"], 34 | "comma-spacing": [2, { "before": false, "after": true }], 35 | "comma-style": [2, "last"], 36 | "constructor-super": 2, 37 | "curly": [2, "multi-line"], 38 | "dot-location": [2, "property"], 39 | "eol-last": 2, 40 | "eqeqeq": [2, "allow-null"], 41 | "generator-star-spacing": [2, { "before": true, "after": true }], 42 | "handle-callback-err": [2, "^(err|error)$" ], 43 | "indent": [2, 2, { "SwitchCase": 1 }], 44 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 45 | "keyword-spacing": [2, { "before": true, "after": true }], 46 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 47 | "new-parens": 2, 48 | "no-array-constructor": 2, 49 | "no-caller": 2, 50 | "no-class-assign": 2, 51 | "no-cond-assign": 2, 52 | "no-const-assign": 2, 53 | "no-control-regex": 2, 54 | "no-debugger": 2, 55 | "no-delete-var": 2, 56 | "no-dupe-args": 2, 57 | "no-dupe-class-members": 2, 58 | "no-dupe-keys": 2, 59 | "no-duplicate-case": 2, 60 | "no-empty-character-class": 2, 61 | "no-eval": 2, 62 | "no-ex-assign": 2, 63 | "no-extend-native": 2, 64 | "no-extra-bind": 2, 65 | "no-extra-boolean-cast": 2, 66 | "no-extra-parens": [2, "functions"], 67 | "no-fallthrough": 2, 68 | "no-floating-decimal": 2, 69 | "no-func-assign": 2, 70 | "no-implied-eval": 2, 71 | "no-inner-declarations": [2, "functions"], 72 | "no-invalid-regexp": 2, 73 | "no-irregular-whitespace": 2, 74 | "no-iterator": 2, 75 | "no-label-var": 2, 76 | "no-labels": 2, 77 | "no-lone-blocks": 2, 78 | "no-mixed-spaces-and-tabs": 2, 79 | "no-multi-spaces": 2, 80 | "no-multi-str": 2, 81 | "no-multiple-empty-lines": [2, { "max": 1 }], 82 | "no-native-reassign": 0, 83 | "no-negated-in-lhs": 2, 84 | "no-new": 2, 85 | "no-new-func": 2, 86 | "no-new-object": 2, 87 | "no-new-require": 2, 88 | "no-new-wrappers": 2, 89 | "no-obj-calls": 2, 90 | "no-octal": 2, 91 | "no-octal-escape": 2, 92 | "no-proto": 0, 93 | "no-redeclare": 2, 94 | "no-regex-spaces": 2, 95 | "no-return-assign": 2, 96 | "no-self-compare": 2, 97 | "no-sequences": 2, 98 | "no-shadow-restricted-names": 2, 99 | "no-spaced-func": 2, 100 | "no-sparse-arrays": 2, 101 | "no-this-before-super": 2, 102 | "no-throw-literal": 2, 103 | "no-trailing-spaces": 0, 104 | "no-undef": 2, 105 | "no-undef-init": 2, 106 | "no-unexpected-multiline": 2, 107 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 108 | "no-unreachable": 2, 109 | "no-unused-vars": [2, { "vars": "all", "args": "none" }], 110 | "no-useless-call": 0, 111 | "no-with": 2, 112 | "one-var": [0, { "initialized": "never" }], 113 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], 114 | "padded-blocks": [0, "never"], 115 | "quotes": [2, "single", "avoid-escape"], 116 | "radix": 2, 117 | "semi": [2, "always"], 118 | "semi-spacing": [2, { "before": false, "after": true }], 119 | "space-before-blocks": [2, "always"], 120 | "space-before-function-paren": [2, "never"], 121 | "space-in-parens": [2, "never"], 122 | "space-infix-ops": 2, 123 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 124 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], 125 | "use-isnan": 2, 126 | "valid-typeof": 2, 127 | "wrap-iife": [2, "any"], 128 | "yoda": [2, "never"] 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | * text eol=lf 3 | 4 | # binaries 5 | *.ai binary 6 | *.psd binary 7 | *.jpg binary 8 | *.gif binary 9 | *.png binary 10 | *.jpeg binary -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Numerous always-ignore extensions 2 | *.DS_Store 3 | *.csv 4 | *.dat 5 | *.diff 6 | *.err 7 | *.gz 8 | *.log 9 | *.orig 10 | *.out 11 | *.pid 12 | *.rar 13 | *.rej 14 | *.seed 15 | *.swo 16 | *.swp 17 | *.vi 18 | *.yo-rc.json 19 | *.zip 20 | *~ 21 | .ruby-version 22 | lib-cov 23 | npm-debug.log 24 | 25 | # Always-ignore dirs 26 | /bower_components/ 27 | /node_modules/ 28 | /temp/ 29 | /tmp/ 30 | /vendor/ 31 | _gh_pages 32 | 33 | # OS or Editor folders 34 | *.esproj 35 | *.komodoproject 36 | .komodotools 37 | *.sublime-* 38 | ._* 39 | .cache 40 | .DS_Store 41 | .idea 42 | .project 43 | .settings 44 | .tmproj 45 | nbproject 46 | Thumbs.db 47 | 48 | # grunt-html-validation 49 | validation-status.json 50 | validation-report.json 51 | 52 | # misc 53 | TODO.md -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | os: 3 | - linux 4 | - osx 5 | - windows 6 | language: node_js 7 | node_js: 8 | - node 9 | - '11' 10 | - '10' 11 | - '9' 12 | - '8' 13 | - '7' 14 | - '6' 15 | - '5' 16 | - '4' 17 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | ## Heads up! 2 | 3 | Breaking changes in v1.0.0! See the [Release History](#CHANGELOG.md). 4 | 5 | ## Usage 6 | 7 | ```js 8 | const omitEmpty = require('{%= name %}'); 9 | 10 | console.log(omitEmpty({ a: 'a', b: '' })); 11 | //=> { a: 'a' } 12 | 13 | console.log(omitEmpty({ a: 'a', b: { c: 'c', d: '' } })); 14 | //=> { a: 'a', b: { c: 'c' } } 15 | 16 | console.log(omitEmpty({ a: ['a'], b: [] })); 17 | //=> { a: ['a'] } 18 | 19 | console.log(omitEmpty({ a: 0, b: 1 })); 20 | //=> { a: 0, b: 1 } 21 | 22 | // set omitZero to true, to evaluate "0" as falsey 23 | console.log(omitEmpty({ a: 0, b: 1 }, { omitZero: true })); 24 | //=> { b: 1 } 25 | ``` 26 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Release history 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) 6 | and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). 7 | 8 |
9 | Guiding Principles 10 | 11 | - Changelogs are for humans, not machines. 12 | - There should be an entry for every single version. 13 | - The same types of changes should be grouped. 14 | - Versions and sections should be linkable. 15 | - The latest version comes first. 16 | - The release date of each versions is displayed. 17 | - Mention whether you follow Semantic Versioning. 18 | 19 |
20 | 21 |
22 | Types of changes 23 | 24 | Changelog entries are classified using the following labels _(from [keep-a-changelog](http://keepachangelog.com/)_): 25 | 26 | - `Added` for new features. 27 | - `Changed` for changes in existing functionality. 28 | - `Deprecated` for soon-to-be removed features. 29 | - `Removed` for now removed features. 30 | - `Fixed` for any bug fixes. 31 | - `Security` in case of vulnerabilities. 32 | 33 |
34 | 35 | ## [1.0.0] - 2018-12-10 36 | 37 | **Changed** 38 | 39 | - refactored completely 40 | - support was removed for `exclude` and `excludeType` 41 | - `options.noZero` was renamed to `options.omitZero` 42 | 43 | ## [0.4.1] - 2016-06-01 44 | 45 | - adds support for excluding types or keys 46 | 47 | ## [0.4.0] - 2016-06-01 48 | 49 | - Changes for removing empty properties inside an array. 50 | - Merge pull request #4 from kakadiadarpan/master 51 | - closes https://github.com/jonschlinkert/omit-empty/issues/3 52 | 53 | ## [0.3.6] - 2016-04-09 54 | 55 | - make array value handling stricter 56 | 57 | ## [0.3.5] - 2016-04-09 58 | 59 | - ensure empty arrays are omitted 60 | 61 | ## [0.3.4] - 2016-03-27 62 | 63 | - ensure zero-length function values aren't omitted 64 | 65 | ## [0.3.3] - 2016-03-19 66 | 67 | - bugfix: do not omit Date objects. 68 | - Merge pull request #2 from stnever/do-not-omit-dates 69 | 70 | ## [0.3.2] - 2015-12-24 71 | 72 | - update deps 73 | 74 | ## [0.3.1] - 2015-03-24 75 | 76 | - first commit 77 | 78 | [Unreleased]: https://github.com/jonschlinkert/omit-empty/compare/0.4.1...1.0.0 79 | [0.4.1]: https://github.com/jonschlinkert/omit-empty/compare/0.4.0...0.4.1 80 | [0.4.0]: https://github.com/jonschlinkert/omit-empty/compare/0.3.6...0.4.0 81 | [0.3.6]: https://github.com/jonschlinkert/omit-empty/compare/0.3.5...0.3.6 82 | [0.3.5]: https://github.com/jonschlinkert/omit-empty/compare/0.3.4...0.3.5 83 | [0.3.4]: https://github.com/jonschlinkert/omit-empty/compare/0.3.3...0.3.4 84 | [0.3.3]: https://github.com/jonschlinkert/omit-empty/compare/0.3.2...0.3.3 85 | [0.3.2]: https://github.com/jonschlinkert/omit-empty/compare/0.3.1...0.3.2 86 | 87 | [Unreleased]: https://github.com/jonschlinkert/omit-empty/compare/0.3.1...HEAD 88 | [keep-a-changelog]: https://github.com/olivierlacan/keep-a-changelog 89 | 90 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-present, Jon Schlinkert. 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 | # omit-empty [![NPM version](https://img.shields.io/npm/v/omit-empty.svg?style=flat)](https://www.npmjs.com/package/omit-empty) [![NPM monthly downloads](https://img.shields.io/npm/dm/omit-empty.svg?style=flat)](https://npmjs.org/package/omit-empty) [![NPM total downloads](https://img.shields.io/npm/dt/omit-empty.svg?style=flat)](https://npmjs.org/package/omit-empty) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/omit-empty.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/omit-empty) 2 | 3 | > Recursively omit empty properties from an object. Omits empty objects, arrays, strings or zero. 4 | 5 | Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support. 6 | 7 | ## Install 8 | 9 | Install with [npm](https://www.npmjs.com/): 10 | 11 | ```sh 12 | $ npm install --save omit-empty 13 | ``` 14 | 15 | ## Heads up! 16 | 17 | Breaking changes in v1.0.0! See the [Release History](#CHANGELOG.md). 18 | 19 | ## Usage 20 | 21 | ```js 22 | const omitEmpty = require('omit-empty'); 23 | 24 | console.log(omitEmpty({ a: 'a', b: '' })); 25 | //=> { a: 'a' } 26 | 27 | console.log(omitEmpty({ a: 'a', b: { c: 'c', d: '' } })); 28 | //=> { a: 'a', b: { c: 'c' } } 29 | 30 | console.log(omitEmpty({ a: ['a'], b: [] })); 31 | //=> { a: ['a'] } 32 | 33 | console.log(omitEmpty({ a: 0, b: 1 })); 34 | //=> { a: 0, b: 1 } 35 | 36 | // set omitZero to true, to evaluate "0" as falsey 37 | console.log(omitEmpty({ a: 0, b: 1 }, { omitZero: true })); 38 | //=> { b: 1 } 39 | ``` 40 | 41 | ## About 42 | 43 |
44 | Contributing 45 | 46 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). 47 | 48 |
49 | 50 |
51 | Running Tests 52 | 53 | Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: 54 | 55 | ```sh 56 | $ npm install && npm test 57 | ``` 58 | 59 |
60 | 61 |
62 | Building docs 63 | 64 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ 65 | 66 | To generate the readme, run the following command: 67 | 68 | ```sh 69 | $ npm install -g verbose/verb#dev verb-generate-readme && verb 70 | ``` 71 | 72 |
73 | 74 | ### Related projects 75 | 76 | You might also be interested in these projects: 77 | 78 | * [is-plain-object](https://www.npmjs.com/package/is-plain-object): Returns true if an object was created by the `Object` constructor. | [homepage](https://github.com/jonschlinkert/is-plain-object "Returns true if an object was created by the `Object` constructor.") 79 | * [isobject](https://www.npmjs.com/package/isobject): Returns true if the value is an object and not an array or null. | [homepage](https://github.com/jonschlinkert/isobject "Returns true if the value is an object and not an array or null.") 80 | * [kind-of](https://www.npmjs.com/package/kind-of): Get the native type of a value. | [homepage](https://github.com/jonschlinkert/kind-of "Get the native type of a value.") 81 | 82 | ### Contributors 83 | 84 | | **Commits** | **Contributor** | 85 | | --- | --- | 86 | | 31 | [jonschlinkert](https://github.com/jonschlinkert) | 87 | | 1 | [kakadiadarpan](https://github.com/kakadiadarpan) | 88 | 89 | ### Author 90 | 91 | **Jon Schlinkert** 92 | 93 | * [GitHub Profile](https://github.com/jonschlinkert) 94 | * [Twitter Profile](https://twitter.com/jonschlinkert) 95 | * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert) 96 | 97 | ### License 98 | 99 | Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert). 100 | Released under the [MIT License](LICENSE). 101 | 102 | *** 103 | 104 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on December 10, 2018._ -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const typeOf = require('kind-of'); 4 | 5 | const omitEmpty = (obj, options) => { 6 | let omitZero = options ? options.omitZero : false; 7 | 8 | let omit = value => { 9 | if (Array.isArray(value)) { 10 | value = value.map(v => omit(v)).filter(v => !isEmpty(v, omitZero)); 11 | } 12 | 13 | if (typeOf(value) === 'object') { 14 | let result = {}; 15 | for (let key of Object.keys(value)) { 16 | let val = omit(value[key]); 17 | if (val !== void 0) { 18 | result[key] = val; 19 | } 20 | } 21 | value = result; 22 | } 23 | 24 | if (!isEmpty(value, omitZero)) { 25 | return value; 26 | } 27 | }; 28 | 29 | let res = omit(obj); 30 | if (res === void 0) { 31 | return typeOf(obj) === 'object' ? {} : res; 32 | } 33 | return res; 34 | }; 35 | 36 | function isEmpty(value, omitZero) { 37 | switch (typeOf(value)) { 38 | case 'null': 39 | case 'undefined': 40 | return true; 41 | case 'boolean': 42 | case 'function': 43 | case 'date': 44 | case 'regexp': 45 | return false; 46 | case 'string': 47 | case 'arguments': 48 | return value.length === 0; 49 | case 'file': 50 | case 'map': 51 | case 'set': 52 | return value.size === 0; 53 | case 'number': 54 | return omitZero ? value === 0 : false; 55 | case 'error': 56 | return value.message === ''; 57 | case 'array': 58 | for (let ele of value) { 59 | if (!isEmpty(ele, omitZero)) { 60 | return false; 61 | } 62 | } 63 | return true; 64 | case 'object': 65 | for (let key of Object.keys(value)) { 66 | if (!isEmpty(value[key], omitZero)) { 67 | return false; 68 | } 69 | } 70 | return true; 71 | default: { 72 | return true; 73 | } 74 | } 75 | } 76 | 77 | /** 78 | * Expose `omitEmpty` 79 | */ 80 | 81 | module.exports = omitEmpty; 82 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "omit-empty", 3 | "description": "Recursively omit empty properties from an object. Omits empty objects, arrays, strings or zero.", 4 | "version": "1.0.0", 5 | "homepage": "https://github.com/jonschlinkert/omit-empty", 6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)", 7 | "repository": "jonschlinkert/omit-empty", 8 | "bugs": { 9 | "url": "https://github.com/jonschlinkert/omit-empty/issues" 10 | }, 11 | "license": "MIT", 12 | "files": [ 13 | "index.js" 14 | ], 15 | "main": "index.js", 16 | "engines": { 17 | "node": ">=4" 18 | }, 19 | "scripts": { 20 | "test": "mocha" 21 | }, 22 | "dependencies": { 23 | "kind-of": "^6.0.2" 24 | }, 25 | "devDependencies": { 26 | "gulp-format-md": "^2.0.0", 27 | "mocha": "^5.2.0" 28 | }, 29 | "keywords": [ 30 | "clear", 31 | "delete", 32 | "empty", 33 | "find", 34 | "has", 35 | "hasown", 36 | "javascript", 37 | "js", 38 | "key", 39 | "keys", 40 | "node", 41 | "node-js", 42 | "object", 43 | "omit", 44 | "properties", 45 | "property", 46 | "remove", 47 | "util", 48 | "utilities", 49 | "utility", 50 | "value" 51 | ], 52 | "verb": { 53 | "run": true, 54 | "toc": false, 55 | "layout": "default", 56 | "tasks": [ 57 | "readme" 58 | ], 59 | "plugins": [ 60 | "gulp-format-md" 61 | ], 62 | "related": { 63 | "list": [ 64 | "is-plain-object", 65 | "isobject", 66 | "kind-of" 67 | ] 68 | }, 69 | "lint": { 70 | "reflinks": true 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * omit-empty 3 | * 4 | * Copyright (c) 2014-2015, Jon Schlinkert. 5 | * Licensed under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | require('mocha'); 11 | const typeOf = require('kind-of'); 12 | const assert = require('assert'); 13 | const omitEmpty = require('./'); 14 | 15 | describe('omit-empty', () => { 16 | it('should return non-empty string values', () => { 17 | assert.equal(omitEmpty('foo'), 'foo'); 18 | }); 19 | 20 | it('should return undefined when the value is an empty string', () => { 21 | assert.equal(omitEmpty(''), void 0); 22 | }); 23 | 24 | it('should return non-empty arrays', () => { 25 | assert.deepEqual(omitEmpty(['foo']), ['foo']); 26 | }); 27 | 28 | it('should return undefined when the value is an empty string', () => { 29 | assert.equal(omitEmpty(''), void 0); 30 | }); 31 | 32 | it('should omit empty values from the given object', () => { 33 | let fixture = { one: {}, a: '', b: 'c' }; 34 | assert.deepEqual(omitEmpty(fixture), { b: 'c' }); 35 | }); 36 | 37 | it('should omit deeply nested empty values from the given object', () => { 38 | let fixture = { 39 | foo: [{ a: '' }, { bar: 'baz' }, [{ a: '' }, { bar: 'baz' }]], 40 | one: { two: { three: { four: { abc: { xyz: '' } } }, five: '', six: 'seven' } }, 41 | a: '', 42 | b: 'c' 43 | }; 44 | 45 | assert.deepEqual(omitEmpty(fixture), { 46 | foo: [{ bar: 'baz' }, [{ bar: 'baz' }]], 47 | one: { two: { six: 'seven' } }, 48 | b: 'c' 49 | }); 50 | }); 51 | 52 | it('should omit empty objects.', () => { 53 | assert.deepEqual(omitEmpty({ a: { b: { c: 'foo' }, d: {} } }), { a: { b: { c: 'foo' } } }); 54 | }); 55 | 56 | it('should omit empty objects.', () => { 57 | assert.deepEqual(omitEmpty({ a: undefined, b: 'c' }), { b: 'c' }); 58 | assert.deepEqual(omitEmpty({ a: null, b: 'c' }), { b: 'c' }); 59 | assert.deepEqual(omitEmpty({ a: '', b: 'c' }), { b: 'c' }); 60 | }); 61 | 62 | it('should omit nested empty objects.', () => { 63 | assert.deepEqual(omitEmpty({ a: { b: undefined, c: 'd' } }), { a: { c: 'd' }}); 64 | assert.deepEqual(omitEmpty({ a: { b: null, c: 'd' } }), { a: { c: 'd' }}); 65 | assert.deepEqual(omitEmpty({ a: { b: '', c: 'd' } }), { a: { c: 'd' }}); 66 | }); 67 | 68 | it('should deeply omit nested empty objects.', () => { 69 | assert.deepEqual(omitEmpty({ a: { b: undefined, c: void 0 } }), {}); 70 | assert.deepEqual(omitEmpty({ a: { b: null, c: void 0 } }), {}); 71 | assert.deepEqual(omitEmpty({ a: { b: '', c: void 0 } }), {}); 72 | }); 73 | 74 | it('should not omit functions', () => { 75 | let fn = (a, b, c) => {}; 76 | let fn2 = () => {}; 77 | assert.deepEqual(omitEmpty({ a: fn, b: fn2 }), { a: fn, b: fn2 }); 78 | }); 79 | 80 | it('should omit empty strings.', () => { 81 | assert.deepEqual(omitEmpty({ a: { b: { c: 'foo' }, d: '' } }), { a: { b: { c: 'foo' } } }); 82 | }); 83 | 84 | it('should omit empty Map', () => { 85 | assert.deepEqual(omitEmpty({ a: { b: { c: 'foo' }, d: new Map() } }), { a: { b: { c: 'foo' } } }); 86 | }); 87 | 88 | it('should omit empty Set', () => { 89 | assert.deepEqual(omitEmpty({ a: { b: { c: 'foo' }, d: new Set() } }), { a: { b: { c: 'foo' } } }); 90 | }); 91 | 92 | it('should not omit regex', () => { 93 | assert.deepEqual(omitEmpty({ a: { b: { c: /foo/ }, d: new Set() } }), { a: { b: { c: /foo/ } } }); 94 | }); 95 | 96 | it('should omit empty arrays.', () => { 97 | assert.deepEqual(omitEmpty({ a: { b: { c: 'foo', d: [] }, foo: [] } }), { a: { b: { c: 'foo' } } }); 98 | assert.deepEqual(omitEmpty({ a: { b: { c: 'foo', d: [void 0] }, foo: [null] } }), { a: { b: { c: 'foo' } } }); 99 | assert.deepEqual(omitEmpty({ a: { b: { c: 'foo', d: [''] }, foo: [null] } }), { a: { b: { c: 'foo' } } }); 100 | assert.deepEqual(omitEmpty({ a: { z: [''], b: { c: 'foo', d: [''] }, foo: [null] } }), { a: { b: { c: 'foo' } } }); 101 | }); 102 | 103 | it('should not omit zero', () => { 104 | let actual = omitEmpty({ a: { b: { c: 'foo', d: 0 }, foo: [] } }); 105 | let expected = { a: { b: { c: 'foo', d: 0 } } }; 106 | assert.deepEqual(actual, expected); 107 | }); 108 | 109 | it('should omit zero when omitZero is true', () => { 110 | let expected = { a: { b: { c: 'foo' } } }; 111 | let actual = omitEmpty({ a: { b: { c: 'foo', d: 0 }, foo: [] } }, { omitZero: true }); 112 | assert.deepEqual(actual, expected); 113 | }); 114 | 115 | it('should not omit boolean false', () => { 116 | let actual = omitEmpty({ a: { b: { c: 'foo', d: 0 }, foo: [], bar: false } }); 117 | let expected = { a: { b: { c: 'foo', d: 0 }, bar: false } }; 118 | assert.deepEqual(actual, expected); 119 | }); 120 | 121 | it('should not omit Dates', () => { 122 | let today = new Date(); 123 | let actual = omitEmpty({ a: { b: { c: 'foo', d: today }, foo: [], bar: false } }); 124 | let expected = { a: { b: { c: 'foo', d: today }, bar: false } }; 125 | assert.deepEqual(actual, expected); 126 | }); 127 | 128 | it('should omit deeply nested values', () => { 129 | let o = { 130 | a: { 131 | b: { c: 'foo', d: 0, e: { f: { g: {}, h: { i: 'i' } } } }, 132 | foo: [['bar', 'baz'], []], 133 | bar: [], 134 | one: 1, 135 | two: 2, 136 | three: 0 137 | } 138 | }; 139 | 140 | assert.deepEqual(omitEmpty(o), { 141 | a: { 142 | b: { c: 'foo', d: 0, e: { f: { h: { i: 'i' } } } }, 143 | foo: [['bar', 'baz']], 144 | one: 1, 145 | two: 2, 146 | three: 0 147 | } 148 | }); 149 | }); 150 | }); 151 | --------------------------------------------------------------------------------