Skip to content

Move to ESM #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: CI
on:
- push
- pull_request
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
- run: npm install
- run: npm test
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
.idea/
coverage
.idea
.vscode
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
5 changes: 0 additions & 5 deletions .travis.yml

This file was deleted.

3 changes: 3 additions & 0 deletions .xo-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"prettier": true
}
51 changes: 24 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# deep-keys
# deep-keys

[![NPM version][npm-image]][npm-url]
[![Build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![Dependency Status][david-image]][david-url]
[![Build status][github-actions-image]][github-actions-url]
[![License][license-image]][license-url]
[![Downloads][downloads-image]][downloads-url]

Expand All @@ -12,61 +11,59 @@

## Install

```sh
$ npm install --save deep-keys
```shell
npm install deep-keys
```

## Usage

##### `deepKeys(obj, intermediate[optional])`

```js
var deepKeys = require('deep-keys');
import deepKeys from "deep-keys";

var obj1 = {
const obj1 = {
a: 1,
b: { c: 1 },
c: { d: { e: 1 }, f: 1 },
d: { e: { f: { g: 1, h: 2 } } },
e: 2,
f: { g: [] }
f: { g: [] },
};
deepKeys(obj1);
//=> ['a', 'b.c', 'c.d.e', 'c.f', 'd.e.f.g', 'd.e.f.h', 'e', 'f.g']

var obj2 = {
type: 'customer',
const obj2 = {
type: "customer",
details: {
name: 'Ariel', age: 26, address: { city: 'Tel Aviv', country: 'Israel' }
name: "Ariel",
age: 26,
address: { city: "Tel Aviv", country: "Israel" },
},
isActive: true
isActive: true,
};
deepKeys(obj2);
//=> ['type', 'details.name', 'details.age', 'details.address.city', 'details.address.country', 'isActive']

// intermediate example
var obj3 = {a:{b:{c:1}}};
deepKeys(obj3); //=> [ 'a.b.c' ]
const obj3 = { a: { b: { c: 1 } } };
deepKeys(obj3); //=> [ 'a.b.c' ]
deepKeys(obj3, true); //=> [ 'a', 'a.b', 'a.b.c' ]

// Dots in key names get escaped
var obj4 = { 'a.': { b: 1} };
deepKeys(obj4) //=> [ 'a\\..b' ]
const obj4 = { "a.": { b: 1 } };
deepKeys(obj4); //=> [ 'a\\..b' ]
```


## License

MIT © [Ariel Mashraki](https://github.com/a8m)

[npm-image]: https://img.shields.io/npm/v/deep-keys.svg?style=flat-square
[npm-url]: https://npmjs.org/package/deep-keys
[travis-image]: https://img.shields.io/travis/a8m/deep-keys.svg?style=flat-square
[travis-url]: https://travis-ci.org/a8m/deep-keys
[coveralls-image]: https://img.shields.io/coveralls/a8m/deep-keys.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/a8m/deep-keys
[david-image]: http://img.shields.io/david/a8m/deep-keys.svg?style=flat-square
[david-url]: https://david-dm.org/a8m/deep-keys
[license-image]: http://img.shields.io/npm/l/deep-keys.svg?style=flat-square
[github-actions-image]: https://github.com/a8m/deep-keys/actions
[github-actions-url]: https://img.shields.io/github/actions/workflow/status/a8m/deep-keys/main.yml?branch=master&logo=github&label=CI&style=flat-square
[license-image]: https://img.shields.io/npm/l/deep-keys.svg?style=flat-square
[license-url]: LICENSE
[downloads-image]: http://img.shields.io/npm/dm/deep-keys.svg?style=flat-square
[downloads-image]: https://img.shields.io/npm/dm/deep-keys.svg?style=flat-square
[downloads-url]: https://npmjs.org/package/deep-keys

1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function deepKeys(object: any): string[];
54 changes: 29 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';

/**
* @description
* returns {boolean} True if `value` is an `Object` but not `null`
* @param value
* @returns {boolean}
*/
function isObject(value) {
return value !== null && typeof value === 'object' && !(value instanceof Date);
return (
value !== null && typeof value === 'object' && !(value instanceof Date)
);
}

/**
Expand All @@ -16,26 +16,30 @@ function isObject(value) {
* @param {*} value Reference to check.
* @returns {boolean} True if `value` is an `Array`.
*/
var isArray = Array.isArray;
const {isArray} = Array;

function deepKeys(object, stack, parent, intermediate) {
for (const element of Object.keys(object)) {
// Escape . in the element name
const escaped = element.replaceAll('.', '\\.');
// If it's a nested object
if (isObject(object[element]) && !isArray(object[element])) {
// Concatenate the new parent if exist
const p = parent ? parent + '.' + escaped : parent;
// Push intermediate parent key if flag is true
if (intermediate) {
stack.push(parent ? p : escaped);
}

function deepKeys(obj, stack, parent, intermediate) {
Object.keys(obj).forEach(function(el) {
// Escape . in the element name
var escaped = el.replace(/\./g, '\\\.');
// If it's a nested object
if(isObject(obj[el]) && !isArray(obj[el])) {
// Concatenate the new parent if exist
var p = parent ? parent + '.' + escaped : parent;
// Push intermediate parent key if flag is true
if (intermediate) stack.push(parent ? p : escaped);
deepKeys(obj[el], stack, p || escaped, intermediate);
} else {
// Create and save the key
var key = parent ? parent + '.' + escaped : escaped;
stack.push(key)
}
});
return stack
deepKeys(object[element], stack, p || escaped, intermediate);
} else {
// Create and save the key
const key = parent ? parent + '.' + escaped : escaped;
stack.push(key);
}
}

return stack;
}

/**
Expand All @@ -52,6 +56,6 @@ function deepKeys(obj, stack, parent, intermediate) {
* @example
* deepKeys({ 'a.': { b: 1 }) ==> ["a\..b"]
*/
module.exports = function (obj, intermediate) {
return deepKeys(obj, [], null, intermediate);
};
export default function _deepKeys(object, intermediate) {
return deepKeys(object, [], null, intermediate);
}
4 changes: 4 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {expectType} from 'tsd';
import deepKeys from './index.js';

expectType<string[]>(deepKeys({foo: {bar: {baz: 'qux'}}}));
16 changes: 11 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"name": "deep-keys",
"version": "0.5.0",
"description": "Creates an array composed of the own enumerable property names(including nested) of an object.",
"type": "module",
"exports": "./index.js",
"types": "./index.d.ts",
"license": "MIT",
"repository": "a8m/deep-keys",
"author": {
Expand All @@ -10,13 +13,14 @@
"url": "https://github.com/a8m/deep-keys"
},
"engines": {
"node": ">=0.10.0"
"node": ">=18"
},
"scripts": {
"test": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha -- -u exports test.js"
"test": "xo && tsd && c8 ava"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"map",
Expand All @@ -28,7 +32,9 @@
"nested-keys"
],
"devDependencies": {
"istanbul": "^0.3.2",
"mocha": "^3.5.3"
"ava": "^6.2.0",
"c8": "^10.1.3",
"tsd": "^0.31.2",
"xo": "^0.60.0"
}
}
Loading