array-to-obj 1.0.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -1,32 +1,30 @@
1
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
2
-
3
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
4
-
1
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
2
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
3
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
4
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
5
+ function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
5
6
  const isString = key => typeof key === 'string';
6
-
7
7
  module.exports = (input, customOptions = {}) => {
8
8
  if (!Array.isArray(input)) {
9
9
  throw new TypeError(`Expected an array, got ${typeof input}`);
10
10
  }
11
-
12
11
  const defaultOptions = {
13
12
  key: 'id'
14
13
  };
15
-
16
- const options = _objectSpread({}, defaultOptions, customOptions);
17
-
18
- const obj = input.reduce((acc, current, i) => {
14
+ const options = _objectSpread(_objectSpread({}, defaultOptions), customOptions);
15
+ const object = input.reduce((acc, current, i) => {
19
16
  const {
20
17
  key: optKey
21
18
  } = options;
22
19
  const fallback = i;
23
-
24
20
  const getKey = key => {
25
- return isString(key) && key in current ? current[key] : key(current);
21
+ if (isString(key)) {
22
+ return key in current ? current[key] : undefined;
23
+ }
24
+ return key(current);
26
25
  };
27
-
28
26
  acc[getKey(optKey) || fallback] = current;
29
27
  return acc;
30
28
  }, {});
31
- return obj;
29
+ return object;
32
30
  };
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":["isString","key","module","exports","input","customOptions","Array","isArray","TypeError","defaultOptions","options","_objectSpread","object","reduce","acc","current","i","optKey","fallback","getKey"],"sources":["../index.js"],"sourcesContent":["const isString = key => typeof key === 'string';\n\nmodule.exports = (input, customOptions = {}) => {\n if (!Array.isArray(input)) {\n throw new TypeError(`Expected an array, got ${typeof input}`);\n }\n\n const defaultOptions = {\n key: 'id',\n };\n\n const options = {\n ...defaultOptions,\n ...customOptions,\n };\n\n const object = input.reduce((acc, current, i) => {\n const {key: optKey} = options;\n const fallback = i;\n\n const getKey = key => (isString(key) && key in current\n ? current[key]\n : key(current));\n\n acc[getKey(optKey) || fallback] = current;\n\n return acc;\n }, {});\n\n return object;\n};\n"],"mappings":";;;;;AAAA,MAAMA,QAAQ,GAAGC,GAAG,IAAI,OAAOA,GAAG,KAAK,QAAQ;AAE/CC,MAAM,CAACC,OAAO,GAAG,CAACC,KAAK,EAAEC,aAAa,GAAG,CAAC,CAAC,KAAK;EAC9C,IAAI,CAACC,KAAK,CAACC,OAAO,CAACH,KAAK,CAAC,EAAE;IACzB,MAAM,IAAII,SAAS,CAAC,0BAA0B,OAAOJ,KAAK,EAAE,CAAC;EAC/D;EAEA,MAAMK,cAAc,GAAG;IACrBR,GAAG,EAAE;EACP,CAAC;EAED,MAAMS,OAAO,GAAAC,aAAA,CAAAA,aAAA,KACRF,cAAc,GACdJ,aAAa,CACjB;EAED,MAAMO,MAAM,GAAGR,KAAK,CAACS,MAAM,CAAC,CAACC,GAAG,EAAEC,OAAO,EAAEC,CAAC,KAAK;IAC/C,MAAM;MAACf,GAAG,EAAEgB;IAAM,CAAC,GAAGP,OAAO;IAC7B,MAAMQ,QAAQ,GAAGF,CAAC;IAElB,MAAMG,MAAM,GAAGlB,GAAG,IAAKD,QAAQ,CAACC,GAAG,CAAC,IAAIA,GAAG,IAAIc,OAAO,GAClDA,OAAO,CAACd,GAAG,CAAC,GACZA,GAAG,CAACc,OAAO,CAAE;IAEjBD,GAAG,CAACK,MAAM,CAACF,MAAM,CAAC,IAAIC,QAAQ,CAAC,GAAGH,OAAO;IAEzC,OAAOD,GAAG;EACZ,CAAC,EAAE,CAAC,CAAC,CAAC;EAEN,OAAOF,MAAM;AACf,CAAC","ignoreList":[]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "array-to-obj",
3
- "version": "1.0.0",
4
- "description": "Convert an array of objects into a keyed object",
3
+ "version": "1.2.0",
4
+ "description": "Index an array of objects by a specified key",
5
5
  "license": "MIT",
6
6
  "repository": "github:chrisvogt/array-to-obj",
7
7
  "author": {
@@ -10,7 +10,7 @@
10
10
  "url": "https://www.chrisvogt.me"
11
11
  },
12
12
  "engines": {
13
- "node": ">=6"
13
+ "node": ">=18"
14
14
  },
15
15
  "scripts": {
16
16
  "build": "npm run compile",
@@ -36,24 +36,45 @@
36
36
  "value",
37
37
  "values"
38
38
  ],
39
- "dependencies": {},
40
39
  "devDependencies": {
41
- "@babel/cli": "^7.1.2",
42
- "@babel/core": "^7.1.2",
43
- "@babel/plugin-proposal-object-rest-spread": "^7.0.0",
44
- "@babel/preset-env": "^7.1.0",
45
- "ava": "^0.25.0",
46
- "codecov": "^3.0.0",
47
- "nyc": "^13.1.0",
48
- "xo": "^0.23.0"
40
+ "@babel/cli": "^7.28.6",
41
+ "@babel/core": "^7.29.0",
42
+ "@babel/plugin-transform-object-rest-spread": "^7.28.6",
43
+ "@babel/preset-env": "^7.29.0",
44
+ "ava": "^6.4.1",
45
+ "nyc": "^17.1.0",
46
+ "xo": "^1.2.3"
47
+ },
48
+ "ava": {
49
+ "files": [
50
+ "test.mjs"
51
+ ]
49
52
  },
50
53
  "nyc": {
51
54
  "reporter": [
52
55
  "lcov",
53
56
  "text"
57
+ ],
58
+ "extension": [
59
+ ".js",
60
+ ".mjs"
61
+ ],
62
+ "include": [
63
+ "index.js"
64
+ ],
65
+ "exclude": [
66
+ "test.mjs",
67
+ "coverage/**",
68
+ "babel.config.js",
69
+ "lib/**"
54
70
  ]
55
71
  },
56
72
  "xo": {
57
- "space": true
73
+ "space": true,
74
+ "rules": {
75
+ "unicorn/prefer-module": "off",
76
+ "unicorn/no-array-reduce": "off",
77
+ "unicorn/no-anonymous-default-export": "off"
78
+ }
58
79
  }
59
80
  }
package/readme.md CHANGED
@@ -1,15 +1,13 @@
1
- # array-to-obj [![Build Status](https://travis-ci.org/chrisvogt/array-to-obj.svg?branch=master)](https://travis-ci.org/chrisvogt/array-to-obj) [![codecov](https://codecov.io/gh/chrisvogt/array-to-obj/badge.svg?branch=master)](https://codecov.io/gh/chrisvogt/array-to-obj?branch=master)
1
+ # array-to-obj [![CI](https://github.com/chrisvogt/array-to-obj/actions/workflows/ci.yml/badge.svg)](https://github.com/chrisvogt/array-to-obj/actions/workflows/ci.yml) [![codecov](https://codecov.io/gh/chrisvogt/array-to-obj/badge.svg?branch=main)](https://codecov.io/gh/chrisvogt/array-to-obj?branch=main)
2
2
 
3
3
  > Convert an array of objects into a keyed object
4
4
 
5
-
6
5
  ## Install
7
6
 
8
7
  ```
9
8
  $ npm install array-to-obj
10
9
  ```
11
10
 
12
-
13
11
  ## Usage
14
12
 
15
13
  #### Default key is `id`