Skip to content

shwetasrsh/stdlib-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

stdlib - A standard library for javascript and node.js

This tutorial demonstrates how to use stdlib - the standard library for JavaScript and Node.js — by building a simple table with data of different types and performing aggregate operations on the data.

Use Case

Given a list containing details of people, we will construct a table which contains details of people and perform aggregate functions on the data.

Steps

  1. Install the stdlib library -
$ npm install @stdlib/stdlib
  1. Create a new file - demo.js

  2. Import stdlib modules -

var tabulate = require('@stdlib/utils/tabulate');
var countBy = require('@stdlib/utils/count-by');
var groupBy = require('@stdlib/utils/group-by');
  1. Define the array -
var people = [
{ name: 'Anand', age: 25, country: 'India' },
{ name: 'Bob', age: 24, country: 'Canada' },
{ name: 'Anita', age: 25, country: 'India' },
{ name: 'David', age: 26, country: 'UK' },
{ name: 'Eve', age: 24, country: 'Canada' },
{ name: 'Frank', age: 30, country: 'UK' },
];
  1. Construct a frequency table -
var table = tabulate(people);

/* returns [
  [
    { name: 'Anand', age: 25, country: 'India' },
    1,
    0.16666666666666666
  ],
  [
    { name: 'Bob', age: 32, country: 'Canada' },
    1,
    0.16666666666666666
  ],
  [
    { name: 'Anita', age: 25, country: 'India' },
    1,
    0.16666666666666666
  ],
  [ { name: 'David', age: 40, country: 'UK' }, 1, 0.16666666666666666 ],
  [
    { name: 'Eve', age: 32, country: 'Canada' },
    1,
    0.16666666666666666
  ],
  [ { name: 'Frank', age: 40, country: 'UK' }, 1, 0.16666666666666666 ]
] */
  1. Count the age of people -
var ageCounts = countBy(people, person => person.age);

/* returns { '25': 2, '32': 2, '40': 2 } */
  1. Group the people by the country -
var groupedByCountry = groupBy(people, person => person.country);

/* returns {
  India: [
    { name: 'Anand', age: 25, country: 'India' },
    { name: 'Anita', age: 25, country: 'India' }
  ],
  Canada: [
    { name: 'Bob', age: 32, country: 'Canada' },
    { name: 'Eve', age: 32, country: 'Canada' }
  ],
  UK: [
    { name: 'David', age: 40, country: 'UK' },
    { name: 'Frank', age: 40, country: 'UK' }
  ]
} */
  1. Run your file -
node demo.js

Command Line Utility

In case you want to run as command-line utility then run -

stdlib repl

Run the given commands in the command line in the same order.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published