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

simple-object-factory

v1.1.0

Published

Define and build/create object for test.

Downloads

3

Readme

Simple Object Factory

Define and build/create object.

Installation

# use npm
$ npm install --save simple-object-factory
# use yarn
$ yarn add simple-object-factory

Usage

import ObjectFactory from 'simple-object-factory'
// or, you can import each function individually.
import { define, build, create } from 'simple-object-factory' 

type UserAttribute = { id: number, name: string, age: number }

class User {
  constructor(public attr: UserAttribute) {
  }
}

ObjectFactory
  .define<UserAttribute>('user', ({ id }) => ({
    id: id,
    name: `userName${id}`,
    age: 20
  }))
  .withTrait({
    underAge: () => ({
      age: 10
    }),
    senior: () => ({
      age: 60
    })
  })
  .onCreate((attr: UserAttribute) => new User(attr))


const userAttribute = build('user')
console.log(userAttribute)
// => { id: 1, name: 'userName1', age: 20 }


const user = create('user', ['underAge'])
console.log(user.attr)
// => { age: 10, id: 2, name: 'userName2' }

API

This library is broadly dividing into three functions.

  • define
    It can define a function, receive a context, and returns an object. Following the definition, it can define the trait, resource, and function for creating an object.

  • build
    The function is building the object with a defined function. When building, it can pass the trait and optional values.

  • create
    The function is creating the object with a defined function. To call this function, "onCreate" or "onCreateWithClass" must be called at definition.

Function definitions

define<<T, U = Partial<T>>(key: string, func: ObjectBuilderType<T>) => DefineContext<U>)

DefineContext contains the following functions described below.

  • withTrait
  • onCreate
  • onCreateWithClass

The definition of Context and ObjectBuilderType is as follows.

type Context = {
  key: string
  id: number
  cycleOf: <T>(name: string) => T
}

type ObjectBuilderType<T = any> = (ctx: Context) => T

The values of Context are as follows.

  • key: Its value equals the key, the argument of define.
  • id: It increments every time "define" or "create" is called.
  • cycleOf: Returns the value of the array defined in "withResource".

withTrait(traits: { [key: string]: ObjectBuilderType<T> }) => DefineContext<T>

It can call after ObjectFactory.define(). The argument is the object; the key is trait name, and the value is function receive Context and returns an object.

withResource(resources: { [key: string]: any[] }) => DefineContext<T>

It can call after ObjectFactory.define(). The argument is the object; the key is resource name, and the value is array.

onCreate<U>(func: (object: T) => U) => DefineContext<T>

It can call after ObjectFactory.define(). The argument is the function, receive the value of built and returns any value.

onCreateWithClass<U extends { new(attr: T): any }>(clazz: U) => DefineContext<T>

It equals onCreaate(attr => new clazz)

build<T, U = Partial<T>>(key: string, traitNames?: string[], option?: U) => U & T

Pass the key; it returns the object defined in "define()".

create<T, U = Partial<T>, V = any>(key: string, traitNames?: string[], option?: U) => V

Pass the key; it returns the object defined in "define()" and "onCreate()".