New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

count-objects

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

count-objects

Counts objects for each value

Source
npmnpm
Version
1.2.1
Version published
Weekly downloads
2
-66.67%
Maintainers
1
Weekly downloads
 
Created
Source

count-objects

Counts objects for each value

demo

Earthquakes API

install

npm install count-objects

usage

constructor

const { CountObjects } = require("count-objects");

const objects = [
  {
    fruit: {
      olive: "Arbequina",
      apple: "Lady Alice",
      orange: "Valencia",
    },
  },
  {
    fruit: {
      olive: "Kalamata",
      apple: "Lady Alice",
    },
  },
];

const co = new CountObjects(objects);

count

//  the count result in an object format:
const countObject = co.count();
console.log((countObject);
// {
//   fruit: {
//     olive: { Arbequina: 1, Kalamata: 1 },
//     apple: { 'Lady Alice': 2 },
//     orange: { Valencia: 1 }
//   }
// }

table

// the same data can be presented as a table:
const countTable = co.table();
console.table(countTable);
// ┌─────────┬────────────────┬──────────────┬───────┐
// │ (index) │      key       │    value     │ count │
// ├─────────┼────────────────┼──────────────┼───────┤
// │    0    │ 'fruit.apple'  │ 'Lady Alice' │   2   │
// │    1    │ 'fruit.olive'  │ 'Arbequina'  │   1   │
// │    2    │ 'fruit.olive'  │  'Kalamata'  │   1   │
// │    3    │ 'fruit.orange' │  'Valencia'  │   1   │
// └─────────┴────────────────┴──────────────┴───────┘

add

// add more values:
co.add([
  {
    fruit: {
      orange: "Valencia",
      apple: "Lady Alice",
    },
  },
]);
console.table(co.table());
// ┌─────────┬────────────────┬──────────────┬───────┐
// │ (index) │      key       │    value     │ count │
// ├─────────┼────────────────┼──────────────┼───────┤
// │    0    │ 'fruit.apple'  │ 'Lady Alice' │   3   │
// │    1    │ 'fruit.olive'  │ 'Arbequina'  │   1   │
// │    2    │ 'fruit.olive'  │  'Kalamata'  │   1   │
// │    3    │ 'fruit.orange' │  'Valencia'  │   2   │
// └─────────┴────────────────┴──────────────┴───────┘

addFilter

// add a filter to count only objects with specific value
// the filter format is an array, like in this example:

// count only objects that have
// key:   'fruit.orange'
// value: 'Valencia'
const valenciaFilter = ["fruit", "orange", "Valencia"];
co.addFilter(valenciaFilter);
console.table(co.table());
// ┌─────────┬────────────────┬──────────────┬───────┐
// │ (index) │      key       │    value     │ count │
// ├─────────┼────────────────┼──────────────┼───────┤
// │    0    │ 'fruit.apple'  │ 'Lady Alice' │   2   │
// │    1    │ 'fruit.olive'  │ 'Arbequina'  │   1   │
// │    2    │ 'fruit.olive'  │  'Kalamata'  │   0   │
// │    3    │ 'fruit.orange' │  'Valencia'  │   2   │
// └─────────┴────────────────┴──────────────┴───────┘

// add another filter, now for "Arbequina" olive:
co.addFilter(["fruit", "olive", "Arbequina"]);
console.table(co.table());
// ┌─────────┬────────────────┬──────────────┬───────┐
// │ (index) │      key       │    value     │ count │
// ├─────────┼────────────────┼──────────────┼───────┤
// │    0    │ 'fruit.apple'  │ 'Lady Alice' │   1   │
// │    1    │ 'fruit.olive'  │ 'Arbequina'  │   1   │
// │    2    │ 'fruit.olive'  │  'Kalamata'  │   0   │
// │    3    │ 'fruit.orange' │  'Valencia'  │   1   │
// └─────────┴────────────────┴──────────────┴───────┘

getFilters

// see the current filters:
console.log(co.getFilters());
// [
//   [ 'fruit', 'orange', 'Valencia' ],
//   [ 'fruit', 'olive', 'Arbequina' ]
// ]

clearFilters

// removes all filters
co.clearFilters();
console.log(co.getFilters());
// []

removeFilter

// removes a specific filter if it exists:
co.addFilter(["a", 1]);
co.addFilter(["b", 2]);
co.addFilter(["c", 3]);
console.log(co.getFilters());
// [ [ 'a', 1 ], [ 'b', 2 ], [ 'c', 3 ] ]
co.removeFilter(["b", 2]);
console.log(co.getFilters());
// [ [ 'a', 1 ], [ 'c', 3 ] ]

filtersCount

// returns how many objects were counted after implementing each filter:
const objects = [
  { a: 1, b: 2 },
  { a: 1, b: 2, c: 3 },
  { a: 1, b: 2, c: 3, d: 4 },
  { a: 1, b: 2, c: 3, d: 4, e: 5 },
  { a: 1 },
];
const co = new CountObjects(objects);
co.addFilter(["a", "1"]); // 5 objects
co.addFilter(["b", "2"]); // 4 objects
co.addFilter(["c", "3"]); // 3 objects
co.addFilter(["d", "4"]); // 2 objects
co.addFilter(["e", "5"]); // 1 object
console.log(co.filtersCount());
// [ 5, 4, 3, 2, 1 ]

co.clearFilters();
co.addFilter(["b", "2"]); // 4 objects
co.addFilter(["d", "4"]); // 2 objects
co.addFilter(["c", "3"]); // 2 objects
co.addFilter(["e", "5"]); // 1 object
co.addFilter(["a", "1"]); // 1 object
console.log(co.filtersCount());
/// [ 4, 2, 2, 1, 1 ]

count unique values

// instead of counting unique objects, we can count unique values.
// in this example we count unique colors:

const flowers = [
  {
    color: "black",
    type: "Tulip",
  },
  {
    color: "black",
    type: "Tulip",
  },
  {
    color: "white",
    type: "Tulip",
  },
  {
    type: "Tulip",
  },
];

const uniqueColors = new CountObjects(flowers, { uniqueKey: "color" });
console.table(uniqueColors.table());
// ┌─────────┬────────┬─────────┬───────┐
// │ (index) │  key   │  value  │ count │
// ├─────────┼────────┼─────────┼───────┤
// │    0    │ 'type' │ 'Tulip' │   2   │
// └─────────┴────────┴─────────┴───────┘

// about the unique key ('uniqueKey'):
// 1. it needs to be at the base of the object (not nested)
// 2. it is omitted from the count
// 3. if it is missing, the values in the object are not counted
// 4. it can only be set once, at the constructor

clone

// creates a clone of the countObjects instance
// this can be helpful when setting a state with React
const clone = co.clone();
console.log(clone === co);
// false

Keywords

javascript

FAQs

Package last updated on 22 Mar 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts