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

struct-fakerator

v1.2.5

Published

- [假資料結構產生器](#假資料結構產生器) - [用途](#用途) - [createValueGenerator](#createvaluegenerator) - [createSelectionGenerator](#createselectiongenerator) - [createObjectGenerator](#createobjectgenerator) - [createArrayGenerator](#createarraygenerator)

Downloads

367

Readme

假資料結構產生器

用途

使用 faker js 時是單一純值不能產生結構,需要自己手動組合結構,此專案利用撰寫設定檔的方式來產生一個特定的假資料函數,減少手動組合結構的麻煩。

const test = {
  type: 'obj',
  content: {
    name: {
      type: 'value',
      generateFn: () => 'hello',
    },
    list: {
      type: 'arr',
      len: 5,
      item: {
        type: 'value',
        generateFn: () => 10,
      },
    },
  },
};

const generateFn = createGeneratorByType(test);

console.log(generateFn());

/*
{
  name: "hello"
  list: [10, 10, 10, 10, 10,]
}
*/

createValueGenerator

const generateFn = createValueGenerator({
  type: 'value',
  generateFn: () => 10,
})

console.log(generateFn());

// 10

createSelectionGenerator

const generateFn = createSelectionGenerator({
 type: 'select',
 items: [1, 2, 3, 4, 5],
})

console.log(generateFn());

// 1 or 2 or 3 or 4 or 5

createObjectGenerator

const generateFn = createObjectGenerator({
 type: 'obj',
 content: {
   name: {
     type: 'value',
     generateFn: () => 'hello',
   },
   list: {
     type: 'arr',
     len: 5,
     item: {
       type: 'value',
       generateFn: () => 10,
     }
   }
 }
})

console.log(generateFn());

// {
//   name: 'hello',
//   list: [10, 10, 10, 10, 10]
// }

createArrayGenerator

const generateFn = createArrayGenerator({
 type: 'arr',
 len: 5,
 item: {
   type: 'value',
   generateFn: () => 10,
 }
})

console.log(generateFn());

// [10, 10, 10, 10, 10]

createTupleGenerator

const generateFn = createTupleGenerator({
 type: 'tuple',
 configItems: [
   {
     type: 'value',
     generateFn: () => 10,
   },
   {
     type: 'value',
     generateFn: () => 'hello',
   },
 ]
})

console.log(generateFn());

// [10, 'hello']

createBoundedSeriesGenerator

const generateFn = createBoundedSeriesGenerator({
 type: 'bounded_series',
 upperLimit: 1.1,
 lowerLimit: 0.9,
 createInitValue: () => 100,
 count: 20
})

console.log(generateFn());

// [100 * 0.9 <= num <= 100 * 1.1, 
//  prev * 0.9 <= num <= prev * 1.1,
//  prev * 0.9 <= num <= prev * 1.1,
//  ...] 

擴充

一切的值皆由 value 產生,可以自己創建各種不同亂數函數

flowchart TB
 value --> int
 value --> float
 value --> string
 value --> email
 value --> other[...]

但並不是所有人情況都能自己手動建立函數,有可能是開放給別人使用的服務,沒辦法在使用方建立函數,這時 createGeneratorByType 第二個可以讓製作服務的人帶入自己的擴充,這樣這個函數就能接受更多種型態。

const createIntValueConfig = (option) => createValueConfig(() => faker.number.int(option));
const createEmailValueConfig = (option) => createValueConfig(() => faker.internet.email(option));

const customTypeMatch = (config) => {
  if (config.type === "int") {
    return createIntValueConfig(config.option);
  }
  if (config.type === "email") {
    return createEmailValueConfig(config.option);
  }

  throw Error("error");
};

const config = {
  type: "obj",
  content: {
    name: { type: "value", generateFn: () => "John" },
    age: { type: "int" },
    email: { type: "email" },
  },
};

const result = createGeneratorByType(config, customTypeMatch)();

console.log(result);

/*
  {
    name: "John",
    age: 50,
    email: "[email protected]",
  }
*/