Skip to content

Commit

Permalink
feat: add object compact
Browse files Browse the repository at this point in the history
  • Loading branch information
afeiship committed Jan 17, 2019
1 parent b2f99ab commit 4325f1d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
14 changes: 8 additions & 6 deletions dist/next-compact.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
var global = global || this || window || Function('return this')();
var nx = global.nx || require('next-js-core2');

nx.compact = function(inArray) {
var result = [];
for (var index = 0; index < inArray.length; index++) {
var value = inArray[index];
value && result.push(value);
}
nx.compact = function(inTarget) {
var isary = inTarget instanceof Array;
var result = isary ? [] : {};
nx.each(inTarget, function(key, value) {
if (value) {
isary ? result.push(value) : (result[key] = value);
}
});
return result;
};

Expand Down
2 changes: 1 addition & 1 deletion dist/next-compact.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions src/next-compact.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
var global = global || this || window || Function('return this')();
var nx = global.nx || require('next-js-core2');

nx.compact = function(inArray) {
var result = [];
for (var index = 0; index < inArray.length; index++) {
var value = inArray[index];
value && result.push(value);
}
nx.compact = function(inTarget) {
var isary = inTarget instanceof Array;
var result = isary ? [] : {};
nx.each(inTarget, function(key, value) {
if (value) {
isary ? result.push(value) : (result[key] = value);
}
});
return result;
};

Expand Down
14 changes: 13 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,21 @@ require('../src/next-compact');

describe('nx.remove', function() {
var arr1 = [0, 1, false, 2, '', 3];
var obj = {
name: 'fei',
age: 0,
test: 12,
ni: null,
tes2: undefined
};

test('return truthy vaulle', function() {
test('array: return truthy vaulle', function() {
var result = nx.compact(arr1);
expect(result).toEqual([1, 2, 3]);
});

test('object: return truthy vaulle', function() {
var result = nx.compact(obj);
expect(result).toEqual({ name: 'fei', test: 12 });
});
});

0 comments on commit 4325f1d

Please sign in to comment.