🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

class-loop

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

class-loop

Creates an Iterator for the class, to use in for-of loop

1.0.1
latest
Source
npm
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

Class-Loop

Creates an Iterator for the class, to use in for-of loop

Problem

In Javascript, for-of loop cannot be used in object.

Eg:

class TestClass {
  constructor() {
    this.property0 = 'prop0';
    this.property1 = 'prop1';
  }
}
// Create an instance
const instance = new TestClass();

for (let i of instance) {
  properties.push(i);
}

// Above will throw 'TypeError: instance is not iterable' error

Using 'Class-Loop' module

  • TypeScript

import ClassLoop class.

import { ClassLoop } from 'class-loop';

Using with a class

import { ClassLoop } from 'class-loop';

class TestClass extends ClassLoop {
  property0: string;
  property1: string;
  constructor() {
    super();
    this.property0 = 'prop0';
    this.property1 = 'prop1';
  }
}

const instance = new TestClass();

const properties = [];
for (const i of instance) {
  properties.push(i);
}

console.log(properties);
/* Output
[
  { key: 'property0', value: 'prop0' },
  { key: 'property1', value: 'prop1' }
]
*/
  • Javascript
const { ClassLoop } = require('class-loop');

Using with a class

const { ClassLoop } = require('class-loop');
class TestClass extends ClassLoop {
  constructor() {
    super();
    this.property0 = 'prop0';
    this.property1 = 'prop1';
  }
}

const instance = new TestClass();

const properties = [];
for (let i of instance) {
  properties.push(i);
}

console.log(properties);

/* Output
[
  { key: 'property0', value: 'prop0' },
  { key: 'property1', value: 'prop1' }
]
*/

Override loop function in child class.

  • TypeScript
import { ClassLoop } from 'class-loop';

class TestClass extends ClassLoop {
  property0: string;
  property1: string;
  constructor() {
    super();
    this.property0 = 'prop0';
    this.property1 = 'prop1';
  }

  // Write your own loop function
  loopForIterator(): any {
    let counter = 2;

    return {
      next: (): any => {
        return {
          value: 'test' + counter,
          done: counter <= -1
        };
      }
    };
  }
}

const instance = new TestClass();

const properties = [];
for (let i of instance) {
  properties.push(i);
}

console.log(properties);

/* Output
[ 'test1', 'test0' ]
*/
  • Javascript
const { ClassLoop } = require('class-loop');
class TestClass extends ClassLoop {
  constructor() {
    super();
    this.property0 = 'prop0';
    this.property1 = 'prop1';
  }

  // Write your own loop function
  loopForIterator(): any {
    let counter = 2;

    return {
      next: (): any => {
        return {
          value: 'test' + counter,
          done: counter <= -1
        };
      }
    };
  }
}

const instance = new TestClass();

const properties = [];
for (let i of instance) {
  properties.push(i);
}

console.log(properties);

/* Output
[ 'test1', 'test0' ]
*/

Keywords

Class

FAQs

Package last updated on 10 Nov 2018

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