🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

find-object-paths

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

find-object-paths - npm Package Compare versions

Comparing version

to
1.0.2

16

dist/lib/FindObjectPaths.d.ts

@@ -1,7 +0,11 @@

export declare function findObjectPaths(obj: object | string | number | [], { key, value }: {
export declare type KeyValue = {
key?: string;
value?: string | boolean | number;
}): string | string[] | void;
export declare function findObjectPathsByKey(obj: object | string | number | [], key: string): string | string[] | void;
export declare function findObjectPathsByValue(obj: object | string | number | [], value: string | boolean | number): string | string[] | void;
export declare function findObjectPathsByKeyValue(obj: object | string | number | [], key?: string, value?: string | boolean | number): string | string[] | void;
value?: Value;
};
export declare type Value = string | boolean | number;
export declare type Searchable = object | string | [];
export declare function has(obj: Searchable, key: KeyValue | string, value?: Value): boolean;
export declare function findObjectPaths(obj: Searchable, { key, value }: KeyValue): string | string[] | void;
export declare function findObjectPathsByKey(obj: Searchable, key: string): string | string[] | void;
export declare function findObjectPathsByValue(obj: Searchable, value: Value): string | string[] | void;
export declare function findObjectPathsByKeyValue(obj: Searchable, key?: string, value?: Value): string | string[] | void;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.findObjectPathsByKeyValue = exports.findObjectPathsByValue = exports.findObjectPathsByKey = exports.findObjectPaths = void 0;
exports.findObjectPathsByKeyValue = exports.findObjectPathsByValue = exports.findObjectPathsByKey = exports.findObjectPaths = exports.has = void 0;
function has(obj, key, value) {
if (typeof key === 'string') {
return findPaths(obj, key, value) !== undefined;
}
else {
return findPaths(obj, key.key, key.value) !== undefined;
}
}
exports.has = has;
function findObjectPaths(obj, { key, value }) {

@@ -5,0 +14,0 @@ return findPaths(obj, key, value);

@@ -15,2 +15,14 @@ 'use strict';

});
test('findObjectPaths: has', async () => {
expect((0, FindObjectPaths_1.has)(acmeInc.employees, { key: 'name' })).toBeTruthy();
expect((0, FindObjectPaths_1.has)(acmeInc.employees, { key: 'foo' })).toBeFalsy();
expect((0, FindObjectPaths_1.has)(acmeInc.employees, { key: 'name', value: 'Hugo Boss' })).toBeTruthy();
expect((0, FindObjectPaths_1.has)(acmeInc.employees, { value: 'Hugo Boss' })).toBeTruthy();
expect((0, FindObjectPaths_1.has)(acmeInc.employees, { value: 'William Shakespeare' })).toBeFalsy();
expect((0, FindObjectPaths_1.has)(acmeInc.employees, 'name')).toBeTruthy();
expect((0, FindObjectPaths_1.has)(acmeInc.employees, 'foo')).toBeFalsy();
expect((0, FindObjectPaths_1.has)(acmeInc.employees, 'name', 'Hugo Boss')).toBeTruthy();
expect((0, FindObjectPaths_1.has)(acmeInc.employees, 'name', 'William Shakespeare')).toBeFalsy();
expect((0, FindObjectPaths_1.has)(acmeInc.employees, 'foo', 'bar')).toBeFalsy();
});
test('findObjectPaths: empty object should return undefined', async () => {

@@ -17,0 +29,0 @@ const path = (0, FindObjectPaths_1.findObjectPaths)({}, { key: 'test' });

@@ -0,10 +1,30 @@

export type KeyValue = {
key?: string;
value?: Value;
};
export type Value = string | boolean | number;
export type Searchable = object | string | [];
/**
* Check if an object has a particular key / value combination
* @param obj <Searchable> the object or array
* @param {string | <KeyValue>} key Key can be either an object or a string
* @param {Value} value the value (optional)
*/
export function has(obj: Searchable, key: KeyValue | string, value?: Value): boolean {
if (typeof key === 'string') {
return findPaths(obj, key, value) !== undefined;
} else {
return findPaths(obj, key.key, key.value) !== undefined;
}
}
/**
* Find a single or all path(s) matching to a key or a value passed in an object
* @param obj <object | string | number | []> the object
* @param {object} key string | value string | boolean | number
* @param obj <Searchable> the object
* @param {object} <KeyValue> key string | value Value
*/
export function findObjectPaths(
obj: object | string | number | [],
{key, value}: {key?: string; value?: string | boolean | number}
): string | string[] | void {
export function findObjectPaths(obj: Searchable, {key, value}: KeyValue): string | string[] | void {
return findPaths(obj, key, value);

@@ -15,6 +35,6 @@ }

* Find a single or all path(s) matching to a key in an object
* @param obj <object | string | number | []> the object
* @param obj <Searchable> the object
* @param key <string> the key to search for
*/
export function findObjectPathsByKey(obj: object | string | number | [], key: string): string | string[] | void {
export function findObjectPathsByKey(obj: Searchable, key: string): string | string[] | void {
return findPaths(obj, key);

@@ -25,9 +45,6 @@ }

* Find a single or all path(s) to a value in an object
* @param obj <object | string | number | []> the object
* @param value <string | boolean | number> the value to search for
* @param obj <Searchable> the object
* @param value <Value> the value to search for
*/
export function findObjectPathsByValue(
obj: object | string | number | [],
value: string | boolean | number
): string | string[] | void {
export function findObjectPathsByValue(obj: Searchable, value: Value): string | string[] | void {
return findPaths(obj, undefined, value);

@@ -38,11 +55,7 @@ }

* Find a or all path(s) to a key with a given value in an object
* @param obj <object | string | number | []> the object
* @param obj <Searchable> the object
* @param key <string> the key to search for
* @param value <string | boolean | number> the value to search for
* @param value <Value> the value to search for
*/
export function findObjectPathsByKeyValue(
obj: object | string | number | [],
key?: string,
value?: string | boolean | number
): string | string[] | void {
export function findObjectPathsByKeyValue(obj: Searchable, key?: string, value?: Value): string | string[] | void {
return findPaths(obj, key, value);

@@ -53,11 +66,7 @@ }

* Find a or all path(s) to a key with a given value in an object
* @param obj <object | string | number | []> the object
* @param obj <Searchable> the object
* @param key <string> the key to search for
* @param value <string | boolean | number> the value to search for
* @param value <Value> the value to search for
*/
function findPaths(
obj: object | string | number | [],
key?: string,
value?: string | boolean | number
): string | string[] | void {
function findPaths(obj: Searchable, key?: string, value?: Value): string | string[] | void {
const results: string[] = [];

@@ -68,3 +77,3 @@

searchKey: string | undefined,
searchValue: string | boolean | number | undefined,
searchValue: Value | undefined,
pathToData: string

@@ -71,0 +80,0 @@ ): string | void => {

{
"name": "find-object-paths",
"version": "1.0.1",
"version": "1.0.2",
"description": "Find paths in objects to given key('s), value('s) or key/value combinations",

@@ -5,0 +5,0 @@ "main": "dist/lib/FindObjectPaths.js",

@@ -1,2 +0,2 @@

[![NPM](https://nodei.co/npm/findObjectPaths.png)](https://nodei.co/npm/findObjectPaths/)
[![NPM](https://nodei.co/npm/find-object-paths.png)](https://nodei.co/npm/find-object-paths/)

@@ -12,2 +12,4 @@ [![CircleCI](https://circleci.com/gh/maugenst/getPaths.svg?style=shield)](https://circleci.com/gh/maugenst/getPaths)

Despite other projects you also can specify values to be searched for.
## Installation

@@ -44,3 +46,3 @@

```ts
import { findObjectPaths } from 'findObjectPaths';
import { has, findObjectPaths } from 'findObjectPaths';

@@ -68,2 +70,9 @@ class TestMe {

// employees[0].isCEO
has(acmeInc, 'name', 'Hugo Boss');
// true
has(acmeInc, {value: 'Hugo Boss'});
// true
}

@@ -78,3 +87,3 @@ }

## Convenient Methods
+ **has** Checks if a key / value combination is available in an object
+ **findObjectPathsByKey** Find a single or all path(s) matching to a key in an object

@@ -106,1 +115,5 @@ + **findObjectPathsByValue** Find a single or all path(s) to a value in an object.

Tests can be found in `/test` and run by jest. To run the tests call ``npm test``.
### Changes
+ added `has` function to check availability of keys / values in objects or arrays

@@ -6,2 +6,3 @@ 'use strict';

import {
has,
findObjectPaths,

@@ -15,3 +16,3 @@ findObjectPathsByKey,

describe('Find-Object-Paths Tests', function () {
let acmeInc = {};
let acmeInc: any = {};
let rawFileContent: string;

@@ -24,2 +25,15 @@

test('findObjectPaths: has', async () => {
expect(has(acmeInc.employees, {key: 'name'})).toBeTruthy();
expect(has(acmeInc.employees, {key: 'foo'})).toBeFalsy();
expect(has(acmeInc.employees, {key: 'name', value: 'Hugo Boss'})).toBeTruthy();
expect(has(acmeInc.employees, {value: 'Hugo Boss'})).toBeTruthy();
expect(has(acmeInc.employees, {value: 'William Shakespeare'})).toBeFalsy();
expect(has(acmeInc.employees, 'name')).toBeTruthy();
expect(has(acmeInc.employees, 'foo')).toBeFalsy();
expect(has(acmeInc.employees, 'name', 'Hugo Boss')).toBeTruthy();
expect(has(acmeInc.employees, 'name', 'William Shakespeare')).toBeFalsy();
expect(has(acmeInc.employees, 'foo', 'bar')).toBeFalsy();
});
test('findObjectPaths: empty object should return undefined', async () => {

@@ -26,0 +40,0 @@ const path = findObjectPaths({}, {key: 'test'});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet