Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Avoid to submit empty params * Documentation changes * Delete any empty key. Move object functions to a helper * Improve descriptions
- Loading branch information
1 parent
a29ec5c
commit 1dc3a2b
Showing
7 changed files
with
152 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import * as object from "./object"; | ||
|
||
export default { | ||
object, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import * as object from "./object"; | ||
|
||
describe("isEmptyDeep", () => { | ||
[ | ||
{ object: {}, isEmpty: true }, | ||
{ object: { a: 1 }, isEmpty: false }, | ||
{ object: { a: { b: "" } }, isEmpty: true }, | ||
{ object: { a: { c: { d: "" } } }, isEmpty: true }, | ||
{ object: { a: { c: { d: "a" } } }, isEmpty: false }, | ||
{ object: { a: { c: { d: [] } } }, isEmpty: true }, | ||
{ object: { a: { c: { d: {} } } }, isEmpty: true }, | ||
{ object: { a: { c: { d: "a" }, d: "" } }, isEmpty: false }, | ||
].forEach(t => { | ||
it(`${JSON.stringify(t.object)} should be ${t.isEmpty ? "empty" : "not empty"}`, () => { | ||
expect(object.isEmptyDeep(t.object)).toBe(t.isEmpty); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("removeEmptyField", () => { | ||
[ | ||
{ in: {}, out: {} }, | ||
{ in: { a: 1 }, out: { a: 1 } }, | ||
{ in: { a: { b: "" } }, out: {} }, | ||
{ in: { a: { b: "" }, c: 1 }, out: { c: 1 } }, | ||
{ in: { a: { c: { d: "" } } }, out: {} }, | ||
{ in: { a: { c: { d: "a" } } }, out: { a: { c: { d: "a" } } } }, | ||
{ in: { a: { c: { d: [] } } }, out: {} }, | ||
{ in: { a: { c: { d: {} } } }, out: {} }, | ||
].forEach(t => { | ||
it(`${JSON.stringify(t.in)} should be simplified to ${JSON.stringify(t.out)}`, () => { | ||
expect(object.removeEmptyFields(t.in)).toEqual(t.out); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import * as _ from "lodash"; | ||
|
||
// Check if all the keys of an object are empty. If any value of the | ||
// object is a nested object it recursively checks if the inner object | ||
// is empty. | ||
export function isEmptyDeep(obj: any): boolean { | ||
if (typeof obj === "number") { | ||
// lodash function isEmpty(number) return true | ||
// but we should not consider it empty | ||
return false; | ||
} | ||
if (typeof obj === "object" && !_.isEmpty(obj)) { | ||
// Check if nested objects are empty | ||
// If some of the keys are not empty the result is not empty | ||
return !Object.keys(obj).some(k => { | ||
return !isEmptyDeep(obj[k]); | ||
}); | ||
} | ||
return _.isEmpty(obj); | ||
} | ||
|
||
// Remove empty keys from an object (recursively) | ||
export function removeEmptyFields(obj: object) { | ||
const res = { ...obj }; | ||
Object.keys(res).forEach(k => { | ||
if (isEmptyDeep(res[k])) { | ||
delete res[k]; | ||
} | ||
}); | ||
return res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters