npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

oq

v0.4.1

Published

Deep object queries

Downloads

5

Readme

Deep object queries

npm install oq

General use case is getting same property from large amount of similar documents. oq will be slow if you'll use lot of different queries because it need a time for compile fetching plan. But if you want to select something like a.b.c.d.e[*].f[1:3].g.h[1,2,3,4] from thousands of documents — oq is better choice.

WARNING API for oq.set() has changed, setter now requires object as first parameter and value as second. Now there is no more oq.clone(). Added new method oq.patch(). See docs for more information.

Usage

import oq from 'oq';

let users = [
    {id: 1, name: 'Frodo'},
    {id: 2, name: 'Samwise'},
    {id: 3, name: 'Peregrin'},
    {id: 4, name: 'Meriadoc'}
];

// get
let getter = oq.get('id');
console.log(users.map(getter)); // [1, 2, 3, 4]

// set
let setter = oq.set('[]'); // push new item
users = setter(users, {id: 5, name: 'Aragorn'});
console.log(users); // [hobbits..., {id: 5, name: 'Aragorn'}]

// functions also can be values
let bagginsfication = oq.set('name', (name) => name + ' Baggins');
users = bagginsfication(users[0]);
console.log(users[0].name); // "Frodo Baggins"

Get

// get all items in array
let getter = oq.get('[*]');

// get 0, 1, and 5 items
let getter = oq.get('[0, 1, 5]');

// get 0 to 5 (not inclusive) items
let getter = oq.get('[0:5]');

// get "a" property
let getter = oq.get('a');

// get "a.b.c" property
let getter = oq.get('a.b.c');

// get first 3 children of "a"
let getter = oq.get('a[0:3]');

// do your stuff with getter
let res = getter(myObj);

Set

// set all items to value
let setter = oq.set('[*]');
// or
let setter = oq.set('[*]', myVal);

// set 0, 1 and 5 items to value
let setter = oq.set('[0, 1, 5]');
// or
let setter = oq.set('[0, 1, 5]', myVal);

// set items from 0 to 5 (not inclusive)
let setter = oq.set('[0:5]');
// or
let setter = oq.set('[0:5]', myVal);

// add new item to array
let setter = oq.set('[]');
// or
let setter = oq.set('[]', myVal);

// set a.b.c property
let setter = oq.set('a.b.c');
// or
let setter = oq.set('a.b.c', myVal);

// do your stuff with setter
setter(myObj, myVal);
// or
setter(myObj); // if value already bound with set(query, value)

Note: oq.set() is extremely slow because it need to copy source object and return new. If you not worrying about modifying source object you can use oq.patch(), it has same API as oq.set() but no returns new object.

Query syntax

  • a — get/set a property from object;
  • a.b — get/set b property from property a of object;
  • [*] — get/set all items in array;
  • [*].a — get/set a properties from all objects in array;
  • a[*] — get/set all items from a property which should be an array;
  • [1:3] — get/set items from 1 to 3 (not inclusive);
  • [1,2,3] — get/set 1, 2 and 3 items in array;
  • [] (only for setter) — push new item to array;

You can pass an array for query like [true, 'a', 1, [1, 2, 3]] instead of string "[*].a[1][1,2,3]";

Translation rules:

  • a['a'];
  • a.b['a', 'b'];
  • [*][true];
  • [*].a[true, 'a'];
  • [1:3][{start: 1, end: 3}];
  • [1,2,3][[1, 2, 3]];
  • [][{push: true}];

Use oq.parse() for parsing query string and oq.format() for convert query array to string.

Benchmarks

Get a.b.c property from object 1M times. On Intel(R) Core(TM) i7-3635QM CPU @ 2.40GHz.

=====GET=====
oq: 52ms
oq w/o precompiled getter: 424ms
dref: 1716ms
json-query: 608ms
simple-objet-query: 521ms
object-path: 16439ms
ramda.path: 69ms
=====SET=====
oq: 8173ms
oq w/o precompiled setter: 18573ms
oq.patch(): 145ms
dref: 1901ms
json-query: N/A
simple-objet-query: N/A
object-path: 12218ms
=====OQ METHODS: GET=====
oq.get('a.b.c'): 50ms
oq.get('a.d[*]'): 322ms
oq.get('a.d[*].e'): 436ms
oq.get('a.d[0:3].e'): 452ms
oq.get('a.d[0, 2, 3].e'): 519ms