Skip to content

Commit

Permalink
add accessible Object#hasOwnProperty stage 2 proposal, `Object.hasO…
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Apr 21, 2021
1 parent 8f158cb commit acd0a4a
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Changelog
##### Unreleased
- Added [accessible `Object#hasOwnProperty` stage 2 proposal](https://github.com/tc39/proposal-accessible-object-hasownproperty)
- `Object.hasOwn` method
- Fixed a possible `RegExp` constructor problem with multiple global `core-js` instances

##### 3.10.2 - 2021.04.19
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2031,6 +2031,24 @@ core-js/features/typed-array/find-last-index
[1, 2, 3, 4].findLast(it => it % 2); // => 3
[1, 2, 3, 4].findLastIndex(it => it % 2); // => 2
````
##### [Accessible `Object#hasOwnProperty`](https://github.com/tc39/proposal-accessible-object-hasownproperty)[⬆](#index)
Module [`esnext.object.has-own`](https://github.com/zloirock/core-js/blob/v3.10.2/packages/core-js/modules/esnext.object.has-own.js).
```js
class Object {
hasOwn(object: object, key: PropertyKey): boolean;
}
```
[*CommonJS entry points:*](#commonjs-api)
```
core-js/proposals/accessible-object-hasownproperty
core-js(-pure)/features(/virtual)/object/has-own
```
[*Examples*](http://es6.zloirock.ru/#log(Object.hasOwn(%7B%20foo%3A%2042%20%7D%2C%20'foo'))%3B%20%2F%2F%20%3D%3E%20true%0Alog(Object.hasOwn(%7B%20foo%3A%2042%20%7D%2C%20'bar'))%3B%20%2F%2F%20%3D%3E%20false%0Alog(Object.hasOwn(%7B%7D%2C%20'toString'))%3B%20%2F%2F%20%3D%3E%20false):
```js
Object.hasOwn({ foo: 42 }, 'foo'); // => true
Object.hasOwn({ foo: 42 }, 'bar'); // => false
Object.hasOwn({}, 'toString'); // => false
````
#### Stage 1 proposals[⬆](#index)
[*CommonJS entry points:*](#commonjs-api)
Expand Down
2 changes: 2 additions & 0 deletions packages/core-js-compat/src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,8 @@ const data = {
},
'esnext.number.range': {
},
'esnext.object.has-own': {
},
// TODO: Remove from `core-js@4`
'esnext.object.iterate-entries': {
},
Expand Down
3 changes: 3 additions & 0 deletions packages/core-js-compat/src/modules-by-versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,7 @@ module.exports = {
'esnext.typed-array.find-last-index',
'esnext.typed-array.unique-by',
],
3.11: [
'esnext.object.has-own',
],
};
4 changes: 4 additions & 0 deletions packages/core-js/features/object/has-own.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require('../../modules/esnext.object.has-own');
var path = require('../../internals/path');

module.exports = path.Object.hasOwn;
1 change: 1 addition & 0 deletions packages/core-js/features/object/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var parent = require('../../es/object');
require('../../modules/esnext.object.has-own');
require('../../modules/esnext.object.iterate-entries');
require('../../modules/esnext.object.iterate-keys');
require('../../modules/esnext.object.iterate-values');
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/internals/has.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var hasOwnProperty = {}.hasOwnProperty;

module.exports = function (it, key) {
module.exports = function hasOwn(it, key) {
return hasOwnProperty.call(it, key);
};
8 changes: 8 additions & 0 deletions packages/core-js/modules/esnext.object.has-own.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var $ = require('../internals/export');
var hasOwn = require('../internals/has');

// `Object.hasOwn` method
// https://github.com/tc39/proposal-accessible-object-hasownproperty
$({ target: 'Object', stat: true }, {
hasOwn: hasOwn
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// https://github.com/tc39/proposal-accessible-object-hasownproperty
require('../modules/esnext.object.has-own');
1 change: 1 addition & 0 deletions packages/core-js/stage/2.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require('../proposals/accessible-object-hasownproperty');
require('../proposals/array-find-from-last');
require('../proposals/array-is-template-object');
require('../proposals/iterator-helpers');
Expand Down
2 changes: 2 additions & 0 deletions tests/commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ for (PATH of ['core-js-pure', 'core-js']) {
ok(load('features/object/get-own-property-names')({ q: 42 })[0] === 'q');
ok(load('features/object/get-own-property-symbols')({ [Symbol()]: 42 }).length === 1);
ok(load('features/object/get-prototype-of')([]) === Array.prototype);
ok(load('features/object/has-own')({ foo: 42 }, 'foo'));
ok(load('features/object/is')(NaN, NaN));
ok(load('features/object/is-extensible')({}));
ok(!load('features/object/is-frozen')({}));
Expand Down Expand Up @@ -963,6 +964,7 @@ for (PATH of ['core-js-pure', 'core-js']) {
ok(typeof load('web/url') === 'function');
ok(typeof load('web/url-search-params') === 'function');
ok('setImmediate' in load('web'));
load('proposals/accessible-object-hasownproperty');
load('proposals/array-filtering');
load('proposals/array-find-from-last');
load('proposals/array-is-template-object');
Expand Down
3 changes: 3 additions & 0 deletions tests/compat/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,9 @@ GLOBAL.tests = {
'esnext.number.range': function () {
return Number.range;
},
'esnext.object.has-own': function () {
return Object.hasOwn;
},
// TODO: Remove from `core-js@4`
'esnext.object.iterate-entries': function () {
return Object.iterateEntries;
Expand Down
14 changes: 14 additions & 0 deletions tests/pure/esnext.object.has-own.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import create from 'core-js-pure/features/object/create';
import hasOwn from 'core-js-pure/features/object/has-own';

QUnit.test('Object.hasOwn', assert => {
assert.isFunction(hasOwn);
assert.arity(hasOwn, 2);
assert.name(hasOwn, 'hasOwn');
assert.deepEqual(hasOwn({ q: 42 }, 'q'), true);
assert.deepEqual(hasOwn({ q: 42 }, 'w'), false);
assert.deepEqual(hasOwn(create({ q: 42 }), 'q'), false);
assert.deepEqual(hasOwn(Object.prototype, 'hasOwnProperty'), true);
assert.throws(() => hasOwn(null, 'foo'), TypeError, 'throws on null');
assert.throws(() => hasOwn(undefined, 'foo'), TypeError, 'throws on undefined');
});
14 changes: 14 additions & 0 deletions tests/tests/esnext.object.has-own.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
QUnit.test('Object.hasOwn', assert => {
const { create, hasOwn } = Object;
assert.isFunction(hasOwn);
assert.arity(hasOwn, 2);
assert.name(hasOwn, 'hasOwn');
assert.looksNative(hasOwn);
assert.nonEnumerable(Object, 'hasOwn');
assert.deepEqual(hasOwn({ q: 42 }, 'q'), true);
assert.deepEqual(hasOwn({ q: 42 }, 'w'), false);
assert.deepEqual(hasOwn(create({ q: 42 }), 'q'), false);
assert.deepEqual(hasOwn(Object.prototype, 'hasOwnProperty'), true);
assert.throws(() => hasOwn(null, 'foo'), TypeError, 'throws on null');
assert.throws(() => hasOwn(undefined, 'foo'), TypeError, 'throws on undefined');
});

0 comments on commit acd0a4a

Please sign in to comment.