Bläddra i källkod

Added a utility to read a string by character quants.

Eugene Lazutkin 3 år sedan
förälder
incheckning
eff9b5ea76
1 ändrade filer med 18 tillägg och 1 borttagningar
  1. 18 1
      tests/helpers.mjs

+ 18 - 1
tests/helpers.mjs

@@ -1,6 +1,6 @@
 'use strict';
 
-import {Writable} from 'stream';
+import {Readable, Writable} from 'stream';
 
 export const streamToArray = array =>
   new Writable({
@@ -11,6 +11,23 @@ export const streamToArray = array =>
     }
   });
 
+export const readString = (string, quant) => new Readable({
+  read() {
+    if (isNaN(quant)) {
+      this.push(string);
+    } else if (string instanceof Buffer) {
+      for (let i = 0; i < string.length; i += quant) {
+        this.push(string.slice(i, i + quant));
+      }
+    } else {
+      for (let i = 0; i < string.length; i += quant) {
+        this.push(string.substr(i, quant));
+      }
+    }
+    this.push(null);
+  }
+});
+
 export const delay = (fn, ms = 20) => async (...args) =>
   new Promise((resolve, reject) => {
     setTimeout(() => {