Skip to content

An easy-to-use JavaScript library made for noobs and lazy people!

Notifications You must be signed in to change notification settings

MalikWhitten67/Lazyjs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 

Repository files navigation

Lazy-Javascript

Lazy-javascript is a JavaScript library that provides a collection of methods to manipulate arrays and json data in a functional and lazy way. The Lazy class defined in the above code allows for chaining of array operations in a deferred manner. This means that the operations are not immediately performed on the original array but rather they are executed when the final result is requested. It also offers parallel processing of arrays using the mapAsync and filterAsync methods. The JsonHandler class provides a simple interface for working with JSON data that abstracts away many of the details of caching and sharding. When combined with the chainable methods provided by lazyjs, working with JSON data can become even easier and more intuitive.

Installation

Lazy.js can be installed using npm:

npm i lazy-javascript

Why use Lazy.js?

Lazy evaluation: lazyjs only evaluates the results of operations when necessary, which can help to reduce the amount of memory used when working with large datasets.

Parallel processing: lazyjs provides several methods for performing parallel processing on arrays, such as mapAsync and filterAsync, which can help to speed up operations that would otherwise be performed serially.

Code simplicity: The chainable methods provided by lazyjs can help to simplify code by allowing complex operations to be expressed as a sequence of simple method calls.

Flexibility: Because lazyjs operates on arrays, it can be used with any data source that can be converted to an array, such as JSON data retrieved from a REST API or a local file.

Ease of use: The JsonHandler class provides a simple interface for working with JSON data that abstracts away many of the details of caching and sharding. When combined with the chainable methods provided by lazyjs, working with JSON data can become even easier and more intuitive.

Usage

Example using JsonHandler class to query data and using lazy class to chain array operations:

  const queryData = () => {
    console.log('Querying data from API...');
    return Promise.resolve(jsonData);
  };
  
  // Use the JsonHandler to query the data
  jsonHandler.query(queryData)
    .then((data) => {
      console.log('Retrieved data:', data);
      
      // Use Lazy.js to chain array operations
      const lazy = new Lazy(data);
      const result = lazy
        .filter((person) => person.age > 30)
        .sort((person) => person.name)
        .map((person) => person.name)
        .join(', ');
      
      console.log('Result:', result);
    })
    .catch((error) => {
      console.error('Error:', error);
    });

    /*
      Output:
      Retrieved data: [
     { id: 1, name: 'Alice', age: 25 },
     { id: 2, name: 'Bob', age: 32 },
     { id: 3, name: 'Charlie', age: 18 },
     { id: 4, name: 'David', age: 43 },
     { id: 5, name: 'Emily', age: 28 },
    { id: 6, name: 'Frank', age: 39 },
    { id: 7, name: 'Grace', age: 21 },
    { id: 8, name: 'Henry', age: 37 }
    ]
    Result: Bob, David, Frank, Henry
    */

Example using parralel caching and sharding

const handler = new JsonHandler();

// Define the query functions
const queryFn1 = async () => {
  // Some expensive database query or other operation
  return { id: 1, name: "John Doe", age: 30 };
};

const queryFn2 = async () => {
  // Another expensive operation
  return { id: 2, name: "Jane Doe", age: 28 };
};

const queryFn3 = async () => {
  // Yet another expensive operation
  return { id: 3, name: "Bob Smith", age: 45 };
};

// Parallel caching
handler.queryParallel([queryFn1, queryFn2, queryFn3]).then((results) => {
  console.log(results);
});

// Sharding
handler.queryWithSharding([queryFn1, queryFn2, queryFn3], 2).then((results) => {
  console.log(results);
});

/*
  Output:
  [
  { id: 1, name: 'John Doe', age: 30 },
  { id: 2, name: 'Jane Doe', age: 28 },
  { id: 3, name: 'Bob Smith', age: 45 }
]
[ { queries: [ [Object], [Object] ] }, { queries: [ [Object] ] } ]
[
  { id: 1, name: 'John Doe', age: 30 },
  { id: 2, name: 'Jane Doe', age: 28 },
  { id: 3, name: 'Bob Smith', age: 45 }
]
 
 as you can see we have our queries for each shard and the outputted data 
*/

the use of sharding and caching is very useful when you have a large amount of data to query and you want to speed up the process of querying the data. this can be used for large scale applications!

About

An easy-to-use JavaScript library made for noobs and lazy people!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published