npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@caliatys/array-typer

v1.0.0

Published

Create an array or dictionnary of generic type object

Downloads

1,391

Readme

Build Status Coverage Status

@caliatys/array-typer

A Node.js module that create an array or dictionnary of generic type object from an array of Object

Installation

npm install @caliatys/array-typer --save

Usage

Javascript

Setting up the example

// Let's create our own object type
let MyObject = /** @class */ (function () {
  function MyObject(obj) {
    // Simple object with one property...
    this.private_prop = obj.prop;
  }

  // ...and one getter method
  Object.defineProperty(MyObject.prototype, "prop", {
      get: function () { return this.private_prop; },
      enumerable: true,
      configurable: true
  });
  return MyObject;
}());

Basic usage

let ArrayTyper = require('@caliatys/array-typer').ArrayTyper;
// Let's create an array of untyped object
let untypedArray = [{"prop": 1},{"prop": 2}];
// And type its elemetns
let typedArray = ArrayTyper.asArray(MyObject, untypedArray);
// Each element of the array are typed, so we can now call its methods
for (let typedObj of typedArray)
  console.log(typedObj.prop) // 1 & 2

We can also send the array as a json

let untypedArray = '[{"prop": 1},{"prop": 2}]';
let typedArray = ArrayTyper.asArray(MyObject, untypedArray);
for (let typedObj of typedArray)
  console.log(typedObj.prop);
//Print
// 1
// 2

Sending additional arguments to the constructor

Additional arguments can be sent to the constructor

let typedArray = ArrayTyper.asArray(MyObject, untypedArray, 'several', 'additional', 'arguments');

These arguments are accessed in MyObject constructor like this

function MyObject(obj) {
  this.private_prop = obj.prop;

  let additionalArguments = arguments[1];
  for (let arg of additionalArguments)
    console.log(arg);
}
//Print
// several
// additional
// arguments

Returning a dictionnary

If you'd rather want a dictionnary, you can use asDict providing a function to generate the key

let untypedArray = [{"prop": 1},{"prop": 2}];
let typedDict = ArrayTyper.asDict(MyObject, untypedArray, t => ""+t.prop); //Using stringified MyObject.prop as key

// typedDict structure
// [
//   "1": {"prop": 1},
//   "2": {"prop": 2}
// ]

Of course, json and additionnal arguments are accepted

let untypedArray = '[{"prop": 1},{"prop": 2}]';
let typedDict = ArrayTyper.asDict(MyObject, untypedArray, t => ""+t.prop, 'several', 'additional', 'arguments');

TypeScript

Setting up the example

class MyObject
{
  private _prop : number = -1;
  public get prop() {return this._prop}; 

  constructor(obj: {})
  {
    this._prop = obj.prop;
  }
}

Usage

The module can be used in Typescript

import { ArrayTyper } from '@caliatys/array-typer';
let untypedArray = '[{"prop": 1},{"prop": 2}]';
let typedArray : MyObject[] = ArrayTyper.asArray(MyObject, untypedArray);

Test

npm run test