diff --git a/README.md b/README.md index 84df8afc..23320c5e 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ npm install ext - [`floor10`](docs/math/floor-10.md) - [`round10`](docs/math/round-10.md) - `Object` + - [`clear`](docs/object/clear.md) - [`entries`](docs/object/entries.md) - `Promise` - [`limit`](docs/promise/limit.md) diff --git a/_es5-ext/object/clear.js b/_es5-ext/object/clear.js deleted file mode 100644 index 268cc5b5..00000000 --- a/_es5-ext/object/clear.js +++ /dev/null @@ -1,16 +0,0 @@ -"use strict"; - -var keys = require("./keys"); - -module.exports = function (obj) { - var error; - keys(obj).forEach(function (key) { - try { - delete this[key]; - } catch (e) { - if (!error) error = e; - } - }, obj); - if (error !== undefined) throw error; - return obj; -}; diff --git a/_es5-ext/test/object/clear.js b/_es5-ext/test/object/clear.js deleted file mode 100644 index 5735e796..00000000 --- a/_es5-ext/test/object/clear.js +++ /dev/null @@ -1,13 +0,0 @@ -"use strict"; - -var isEmpty = require("../../object/is-empty"); - -module.exports = function (t, a) { - var x = {}; - a(t(x), x, "Empty: Returns same object"); - a(isEmpty(x), true, "Empty: Not changed"); - x.foo = "raz"; - x.bar = "dwa"; - a(t(x), x, "Same object"); - a(isEmpty(x), true, "Emptied"); -}; diff --git a/docs/object/clear.md b/docs/object/clear.md new file mode 100644 index 00000000..468be1bf --- /dev/null +++ b/docs/object/clear.md @@ -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); // [] +``` diff --git a/object/clear.js b/object/clear.js new file mode 100644 index 00000000..be3be2b2 --- /dev/null +++ b/object/clear.js @@ -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; +}; diff --git a/test/object/clear.js b/test/object/clear.js new file mode 100644 index 00000000..45afed68 --- /dev/null +++ b/test/object/clear.js @@ -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"); + }); + } +});