-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
54 additions
and
29 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,12 @@ | ||
# `Object.clear` _(ext/object/clear)_ | ||
|
||
Deletes all own, enumerable, non-symbol properties in the object | ||
|
||
```javascript | ||
const clear = require("ext/object/clear"); | ||
|
||
const obj = { foo: "bar" }; | ||
|
||
clear(obj); | ||
Object.keys(obj); // [] | ||
``` |
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,15 @@ | ||
"use strict"; | ||
|
||
var ensureObject = require("type/object/ensure") | ||
, ensure = require("type/ensure"); | ||
|
||
var objPropertyIsEnumerable = Object.prototype.propertyIsEnumerable; | ||
|
||
module.exports = function (object) { | ||
ensure(["object", object, ensureObject]); | ||
for (var key in object) { | ||
if (!objPropertyIsEnumerable.call(object, key)) continue; | ||
delete object[key]; | ||
} | ||
return 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,26 @@ | ||
"use strict"; | ||
|
||
var assert = require("chai").assert | ||
, clear = require("../../object/clear"); | ||
|
||
describe("object/clear", function () { | ||
it("Should clear enumerable properties", function () { | ||
var obj = { foo: "bar", elo: "sfds" }; | ||
clear(obj); | ||
// eslint-disable-next-line no-unreachable-loop | ||
for (var key in obj) throw new Error("Unexpected" + key); | ||
}); | ||
it("Should return input object", function () { | ||
var obj = {}; | ||
assert.equal(clear(obj), obj); | ||
}); | ||
if (Object.defineProperty && Object.keys) { | ||
it("Should keep non enumerable properties", function () { | ||
var obj = { foo: "bar", elo: "sfds" }; | ||
Object.defineProperty(obj, "hidden", { value: "some" }); | ||
clear(obj); | ||
assert.deepEqual(Object.keys(obj), []); | ||
assert.equal(obj.hidden, "some"); | ||
}); | ||
} | ||
}); |