Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 12, 2021
1 parent 28c4d17 commit 6013e5f
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 33 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
- 8
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
8 changes: 2 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ Get a random key from an object.
@example
```
import randomObjKey = require('random-obj-key');
import randomObjectKey from 'random-obj-key';
randomObjKey({foo: true, bar: true});
//=> 'bar'
```
*/
declare function randomObjKey<ObjectType extends {[key: string]: any}>(
object: ObjectType
): keyof ObjectType;

export = randomObjKey;
export default function randomObjectKey<ObjectType extends Record<string, any>>(object: ObjectType): keyof ObjectType;
6 changes: 2 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

module.exports = object => {
export default function randomObjectKey(object) {
const keys = Object.keys(object);
return keys[Math.floor(Math.random() * keys.length)];
};
}
4 changes: 2 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expectType} from 'tsd';
import randomObjKey = require('.');
import randomObjectKey from './index.js';

expectType<'foo' | 'bar'>(randomObjKey({foo: true, bar: true}));
expectType<'foo' | 'bar'>(randomObjectKey({foo: true, bar: true}));
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"description": "Get a random key from an object",
"license": "MIT",
"repository": "sindresorhus/random-obj-key",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -30,9 +33,9 @@
"choice"
],
"devDependencies": {
"ava": "^2.4.0",
"stable-fn": "^2.0.0",
"tsd": "^0.9.0",
"xo": "^0.25.3"
"ava": "^3.15.0",
"stable-fn": "^3.0.0",
"tsd": "^0.17.0",
"xo": "^0.44.0"
}
}
5 changes: 1 addition & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,21 @@

> Get a random key from an object

## Install

```
$ npm install random-obj-key
```


## Usage

```js
const randomObjectKey = require('random-obj-key');
import randomObjectKey from 'random-obj-key';

randomObjectKey({foo: true, bar: true});
//=> 'bar'
```


## Related

- [random-obj-prop](https://github.com/sindresorhus/random-obj-prop) - Get a random property from an object
Expand Down
10 changes: 5 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import test from 'ava';
import stableFn from 'stable-fn';
import randomObjectKey from '.';
import stableFunction from 'stable-fn';
import randomObjectKey from './index.js';

const fixture = {
a: true,
b: true,
c: true,
d: true,
e: true
e: true,
};

test('main', t => {
t.false(stableFn(() => randomObjectKey(fixture)));
t.false(stableFunction(() => randomObjectKey(fixture)));

for (let i = 0; i < 1000; i++) {
for (let index = 0; index < 1000; index++) {
t.is(typeof randomObjectKey(fixture), 'string');
}
});

0 comments on commit 6013e5f

Please sign in to comment.