๐Ÿ“ฆ ljharb / own-keys

๐Ÿ“„ index.js ยท 62 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62'use strict';

var test = require('tape');
var hasSymbols = require('has-symbols/shams')();
var hasPropertyDescriptors = require('has-property-descriptors')();

var ownKeys = require('../');

test('ownKeys', function (t) {
	t.equal(typeof ownKeys, 'function', 'is a function');
	t.equal(
		ownKeys.length,
		1,
		'has a length of 1'
	);

	t.test('basics', function (st) {
		var obj = { a: 1, b: 2 };
		if (hasPropertyDescriptors) {
			Object.defineProperty(obj, 'c', {
				configurable: true,
				enumerable: false,
				writable: true,
				value: 3
			});
		}

		st.deepEqual(
			ownKeys(obj).sort(),
			(hasPropertyDescriptors ? ['a', 'b', 'c'] : ['a', 'b']).sort(),
			'includes non-enumerable properties'
		);

		st.end();
	});

	t.test('symbols', { skip: !hasSymbols }, function (st) {
		/** @type {Record<PropertyKey, unknown>} */
		var obj = { a: 1 };
		var sym = Symbol('b');
		obj[sym] = 2;

		var nonEnumSym = Symbol('c');
		Object.defineProperty(obj, nonEnumSym, {
			configurable: true,
			enumerable: false,
			writable: true,
			value: 3
		});

		st.deepEqual(
			ownKeys(obj).sort(),
			['a', sym, nonEnumSym].sort(),
			'works with symbols, both enum and non-enum'
		);

		st.end();
	});

	t.end();
});