Skip to content

Commit

Permalink
update packages and add ts support
Browse files Browse the repository at this point in the history
  • Loading branch information
pooriamo committed Aug 15, 2024
1 parent 486f2a3 commit 486a4b2
Show file tree
Hide file tree
Showing 12 changed files with 11,052 additions and 4,137 deletions.
4 changes: 0 additions & 4 deletions .babelrc

This file was deleted.

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
coverage
.nyc_output
.nyc_output
.idea
19 changes: 19 additions & 0 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"presets": [
"@babel/preset-typescript",
[
"@babel/preset-env",
{
"targets": {
"edge": "17",
"firefox": "60",
"chrome": "67",
"safari": "11.1",
"node": "current"
},
"useBuiltIns": "usage",
"corejs": "3.6.5"
}
]
]
}
12 changes: 12 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
declare type Options<T extends BaseObject, ChildKey extends string> = {
idKey?: keyof T;
parentKey?: keyof T;
childrenKey?: ChildKey;
rootParentsId?: string | number | null;
};
declare type Node<T, ChildrenKey extends string> = T & {
[key in ChildrenKey]: Node<T, ChildrenKey>[];
};
declare type BaseObject = Record<string | symbol | number, unknown>;
export default function nest<T extends BaseObject, ChildKey extends string = 'children'>(array: T[], options?: Options<T, ChildKey>): Node<T, ChildKey>[];
export {};
79 changes: 39 additions & 40 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,55 @@
'use strict';
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

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; }

var defaultOptions = {
exports.default = nest;
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; }
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; }
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; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
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); }
const defaultOptions = {
idKey: 'id',
parentKey: 'parent_id',
childrenKey: 'children',
rootParentsId: null
};

var nest = function nest(array) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

var _defaultOptions$optio = _extends({}, defaultOptions, options),
ID_KEY = _defaultOptions$optio.idKey,
PARENT_KEY = _defaultOptions$optio.parentKey,
CHILDREN_KEY = _defaultOptions$optio.childrenKey,
ROOT_PARENTS_ID = _defaultOptions$optio.rootParentsId;

function nest(array, options) {
var _options$idKey, _options$parentKey, _options$childrenKey, _options$rootParentsI;
const ID_KEY = (_options$idKey = options === null || options === void 0 ? void 0 : options.idKey) !== null && _options$idKey !== void 0 ? _options$idKey : defaultOptions.idKey;
const PARENT_KEY = (_options$parentKey = options === null || options === void 0 ? void 0 : options.parentKey) !== null && _options$parentKey !== void 0 ? _options$parentKey : defaultOptions.parentKey;
const CHILDREN_KEY = (_options$childrenKey = options === null || options === void 0 ? void 0 : options.childrenKey) !== null && _options$childrenKey !== void 0 ? _options$childrenKey : defaultOptions.childrenKey;
const ROOT_PARENTS_ID = (_options$rootParentsI = options === null || options === void 0 ? void 0 : options.rootParentsId) !== null && _options$rootParentsI !== void 0 ? _options$rootParentsI : defaultOptions.rootParentsId;
if (ID_KEY === PARENT_KEY) {
throw new TypeError("The values for '".concat(String(ID_KEY), "' and '").concat(String(PARENT_KEY), "' can not be the same"));
}
if (!Array.isArray(array)) {
throw new TypeError('The input must be an array.');
}

[ID_KEY, PARENT_KEY].forEach(function (key) {
if (array.some(function (el) {
return !Object.prototype.hasOwnProperty.call(el, key);
})) {
throw new TypeError('Found an element with no \'' + key + '\' key');
array.forEach(item => {
if (!Object.prototype.hasOwnProperty.call(item, ID_KEY)) {
throw new TypeError("The item must have an '".concat(String(ID_KEY), "' key"));
}
});

var buildTree = function buildTree(parentId) {
var tree = array.filter(function (item) {
return item[PARENT_KEY] === parentId;
});

if (tree.length > 0) {
tree = tree.map(function (item) {
return _extends({}, item, _defineProperty({}, CHILDREN_KEY, buildTree(item[ID_KEY])));
});
if (!Object.prototype.hasOwnProperty.call(item, PARENT_KEY)) {
throw new TypeError("The item must have an '".concat(String(PARENT_KEY), "' key"));
}

return tree;
if (item[ID_KEY] === item[PARENT_KEY]) {
throw new TypeError("The item cannot have the same '".concat(String(ID_KEY), "' and '").concat(String(PARENT_KEY), "' keys"));
}
if (!isKey(item[ID_KEY])) {
throw new TypeError("The '".concat(String(ID_KEY), "' key must be a string or a number"));
}
});
const buildTree = parentId => {
const tree = array.filter(item => item[PARENT_KEY] === parentId);
return tree.map(item => _objectSpread(_objectSpread({}, item), {}, {
[CHILDREN_KEY]: buildTree(item[ID_KEY])
}));
};

return buildTree(ROOT_PARENTS_ID);
};

exports.default = nest;
}
;
function isKey(value) {
return typeof value === 'string' || typeof value === 'number' || typeof value === 'symbol';
}
Loading

0 comments on commit 486a4b2

Please sign in to comment.