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

generic-factory

v1.0.13

Published

A TypeScript implementation of an abstract factory. Uses dlv for data retrieval.

Downloads

32

Readme

Generic Factory

An abstract factory implementation using TypeScript. Use a single factory to create instances of any class, given raw source data, and a schema.

Basic Usage

Important - before getting started know that for this pattern to work, it's important to commit to the factory being responsible for property intilialization. In other words, any class requested from the factory should have no required constructor params (otherwise you'll receive a type error).

  1. Import exposed classes and schema data structure where needed like this:
import { Factory, schema } from 'generic-factory';
  1. Define the schema pairing a class with fields from a raw dataset. The generic Factory will iterate through your schema to populate a new class instance with all properties you define. Here is an example where I define a schema for a User class:
import { schema } from 'generic-factory';

export const userSchema: schema = [
  {
    property: 'Username',
    keypath: 'node.name'
  },
  {
    property: 'Email',
    keypath: 'node.mail'
  }
];
  1. Use the generic factory. Use factory to instantiate a class by passing a class type, raw data, and a schema.
const user = factory.Create(User, UserData.node, userSchema);

Use the new instance the factory created.

console.log(user.Username);
  1. For more details on this example, reference the test-domain and tests directory for this package in node_modules.

Using a Post Processor

Post processors allow for dynamic changes to be made to raw data before the data is saved to the instance created by the factory. Three post processors are included in this repo (StringToDateProcessor, ValueToBooleanProcessor, & StringToIntProcessor).

StringToDateProcessor example

In the appropriate schema, declare the StringToDateProcessor as shown below:

export const userSchema: schema = [
  {
    property: 'CreatedOn',
    keypath: 'node.created'
    postProcessor: new StringToDateProcessor() // <- like this
  }
]

Using a custom Post Processor

You can declare any class that implements the IProcessable interface to the postProcessor member of a schema. You can develop your own processor, similar the StringToDateProcessor shown below:

import { IProcessable } from 'generic-factory';

export class StringToDateProcessor implements IProcessable {

  public process(data: any): Date {
    let stringData: string = data;
    if (stringData.length === 10) {
      stringData += '000'; // add milisecond precision
    }
    return new Date(parseInt(stringData));
  }
}

You can then use your processor as your would the StringToDateProcessor.

Development Instructions

  1. Pull repo and run npm install
  2. Run tests by running npm run test
  3. Build using npm run build