Skip to content

Commit

Permalink
feat: Object.clear util
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Sep 24, 2021
1 parent 29432cb commit a955da4
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 29 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 0 additions & 16 deletions _es5-ext/object/clear.js

This file was deleted.

13 changes: 0 additions & 13 deletions _es5-ext/test/object/clear.js

This file was deleted.

12 changes: 12 additions & 0 deletions docs/object/clear.md
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); // []
```
15 changes: 15 additions & 0 deletions object/clear.js
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;
};
26 changes: 26 additions & 0 deletions test/object/clear.js
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");
});
}
});

0 comments on commit a955da4

Please sign in to comment.