lazy-datadeprecated

asynchronous way to create data streams

Usage no npm install needed!

<script type="module">
  import lazyData from 'https://cdn.skypack.dev/lazy-data';
</script>

README

lazy-data

What is lazy data ?

Have you ever needed a data stream but you don't know how to do it ?. Well, that's the idea behind lazy data. Lazy data allows you to create data in an async way, giving you some of the functionalities provided by filter and map.

//in a synchronous way when you generate data
var mySyncData = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var results = mySyncData
  .filter(function(times){
    return times > 4;
  })
  .map(function(times){
    return 'I have done this ' + times + ' times';
  });

console.log(results);

Unfortunately, when you don't know how much data you will need, or this data could be generated through time, you can have a big trouble managing memory.

//this can be a never ending stream of data
lazyData(function(x){ return x + 1 }, 0)
.forEach(function(num){
  console.log(num);
});

Creating a stream

This is just as easy as

lazyData(function foo(bar){
  return bar * 2;
}, 3);

Working with your stream

You can process your data using map, filter.

lazyData(function(i){ return i + 1; }, 1)
.filter(function(i){ return i % 3 == 0; })
.map(function(i){ return i + ' times'; });

Collecting your data

To begin collecting your data, you have to call one of two methods available:

//this will bring you each element of data stream step by step
lazyData(function(x){ return x * 2; }, 1)
.forEach(function(val){
  console.log(val);
});

//this will bring all your data at once
/*
  be careful with this, unless you have an ending point(returning null or undefined) or taking a limited set of data,
  it won't work as you expected.
*/
lazyData(function(x){ return x * 2; }, 1)
.take(20)
.toArray(function(array){
  console.log(array)
});

Methods available

  • forEach
  • toArray
  • filter
  • map
  • take
  • throttle

forEach

Used to collect data step by step, it takes each element to work with and process. This is one of the methods that really begins with data creation.

//it does not anything yet, until we call forEach or toArray
var lazy = lazyData(function(num){ return data + 10; }, 1);

//it begins to create infinite data
lazy.forEach(function(num){
  console.log(num);
});

But if you want to stop to creating data if a condition is meet, you can return null or undefined.

lazyData(function(dollar){
  return dollar >= 100 ? null : dollar + 10;
}, 10)
.forEach(function(dollars){
  console.log('I have ' + dollars + ' dollars');
});

toArray

Used to collect all the data created with the stream, it takes the resulting array all at once. This is one of the methods that really begins with data creation.

var lazy = lazyData(function(num){ return data + 10; }, 1);

lazy.toArray(function(array){
  console.log(array);
});

filter

Just like a regular array, you can filter each of its elements before it being given to forEach or toArray

lazyData(function(num){ return num + 1; }, 1)
.filter(function(num){
  return num % 2 != 0;
})
.forEach(function(num){
  console.log(num);//it only shows odd numbers
});

map

As a regular array, you can map each of its elements to transform data to something else.

lazyData(function(num){ return num + 1; }, 1)
.map(function(num){
  return 'I gonna have ' + num + ' kittens in my house';
})
.toArray(function(array){
  console.log(array);
})

take

You can give this stream a limit of how many results you would like to take.

//it will only give you 20 results
lazyData(function(num){ return num + 10; }, 10)
.take(20)
.forEach(function(number){
  console.log('they have give me ' + number + ' coffees');
});

throttle

You can also give a time between each iteration.

lazyData(function(num){ return num + 10 }, 10)
.throttle(400)//400 ms delay between each iteration
.forEach(function(num){
  console.log(num);
});