Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Like a turkey. 🦃

License

NotificationsYou must be signed in to change notification settings

jimbuck/stuffd

Repository files navigation

I like my data how I like my turkeys. Stuffd. 🦃

TravisCI Build StatusAzurePipelines Build StatusCodecovDependenciesDevDependenciesnpmMonthly DownloadsTotal Downloads

Create lots of random data, nested or relational (or both) and do whatever you want with it. Output the JSON, manipulate it in TS/JS land, or pipe each object into your database (both objectMode and standard stream suppported)!

Installation

npm i stuffd

Features:

  • Fluid API or TypeScript Decorators for super smooth model definitions.
  • Easily create heaps of nested or relational data in milliseconds.
  • CLI tool for kickin' back and running tasks.
  • Works perfectly with gulp/grunt/any task runner.
  • Packed full of tests, so dense. Over 230 tests and counting!

Examples

Basic (Nested with Fluent API)

cosnt{Stuffd,Context}=require('stuffd');// Create Typescript-like enums with explicit integers!constModuleType=Stuffd.createEnum({SleepingQuarters:0,DiningRoom:1,RecRoom:2,Agricultural:3,MedicalBay:4,Engineering:5,FitnessCenter:6,ResearchStation:7,NavigationStation:8,WeaponsBay:9,ShieldRoom:10,CargoBay:11});// Create Typescript-like enums with implicit integers!constModuleSize=Stuffd.createEnum(['Small','Medium','Large']);// Create custom, re-usable attributes!constpersonName=(c)=>`${c.first()}${c.last()}`;constManufacturer=Stuffd.create('Manufacturer').key('id',id=>id.guid()).prop('name',n=>n.pick(['Rocketdyne','Areojet','Boeing','Lockheed','SpaceX','Kerbal'])).prop('operatingSince',os=>os.date(newDate('01/01/1950'),newDate())).prop('ceo',c=>c.custom(c=>`${c.first()}${c.last()}`)).prop('founder',f=>f.custom(personName)).build();constEngine=Stuffd.create('Engine').prop('model',m=>m.str(/[A-Z]{1,3}-\d{1,3}[DXS]{0,1}/)).prop('year',y=>y.integer(1967,2020)).prop('thrust',t=>t.float(1,5,12)).prop('mass',m=>m.float(3,200,2000)).prop('manufacturer',m=>m.type(Manufacturer)).toString(function(){return`${this.manufacturer.name}${this.model} (${this.year})`;}).build();constModule=Stuffd.create('Module').prop('type',t=>t.enum(ModuleType,String)).prop('size',s=>s.enum(ModuleSize,Number)).prop('operational',op=>op.bool(3/4)).prop('mass',m=>m.float(3,200,5000)).toString(function(){return`[${ModuleSize[this.size]}]${this.type}${this.operational ?'' :' (undergoing maintenance)'}`;}).build();constSpaceship=Stuffd.create('Spaceship').prop('name',n=>n.str(/((Ares|Athena|Hermes|Icarus|Jupiter|Odyssey|Orion|Daedalus|Falcon|[A-Z]Wing)[XXIIVVCD]{2,3})/)).prop('captain',m=>m.custom(personName)).prop('primaryEngines',pe=>pe.type(Engine)).prop('primaryEngineCount',pec=>pec.integer(1,5)).prop('secondaryEngines',se=>se.type(Engine)).prop('secondaryEngineCount',sec=>sec.integer(0,20)).prop('modules',m=>m.list(Module,3,8)).prop('hullMass',m=>m.float(3,5000,20000)).getter('totalThrust',function(){lettotal=(this.primaryEngines.thrust*this.primaryEngineCount)+(this.secondaryEngines.thrust*this.secondaryEngineCount);returnMath.round(total*10)/10;}).getter('totalMass',function(){lettotal=this.hullMass;total+=this.primaryEngines.mass*this.primaryEngineCount;total+=this.secondaryEngines.mass*this.secondaryEngineCount;this.modules.forEach((m)=>total+=m.mass);returnMath.round(total);}).toString(function(){letstr=[this.name+':'];str.push(`  Crew:`);str.push(`${this.captain}`);str.push(`  Engines:`);str.push(`${this.primaryEngineCount}x${this.primaryEngines}`);if(this.secondaryEngineCount>0)str.push(`${this.secondaryEngineCount}x${this.secondaryEngines}`);str.push(`  Modules:`);str.push('    '+this.modules.map((m)=>m.toString()).join(EOL+'    '));str.push(`  Stats:`);str.push(`    Total Thrust:${this.totalThrust} tons`);str.push(`    Total Mass:${this.totalMass} kg`);returnstr.join(EOL);}).build();// Optionally provide a seed (same seed + same operations = same result)constctx=newContext(2187);constfleet=ctx.create(Spaceship,3);console.log(fleet.map(ship=>ship.toString()).join(''));// Prints out ALL the ship details...

Basic (Relational with Decorators)

import{Stuffd,Context}from'stuffd';import{Key,Guid,Custom,Range,Str,Ref,Integer,Float}from'stuffd/decorators';@Stuffd()classStudent{  @Key() @Guid()id:string;  @Custom(c=>`${c.first()}${c.last()}`)name:string;    @Range(newDate('01/01/1950'),newDate('12/31/2010'))dateOfBirth:Date;  @Str(/\(\d{3}\)\d{3}-\d{4}/)phone:string;}@Stuffd()classTeacher{  @Key() @Guid()id:string;  @Custom(c=>`${c.first()}${c.last()}`)name:string;}@Stuffd()classClass{    @Str(/[A-Z]{2}-\d{4}-[a-e]/)id:string;  @Ref(Teacher)teacherId:string;  @Integer(1,9)period:number;}@Stuffd()classGrade{  @Ref(Student)studentId:string;  @Ref(Class,'id')classId:string;  @Float(1,60,100)grade:number;}constctx=newContext(246);// Create array of 50 students...conststudents=ctx.create(Student,50);// [...]constteachers=ctx.create(Teacher,3);// [...]// Create array of 5 classes referencing a teacher's id from the 3 in that list...constclasses=ctx.using({'teacherId':teachers}).create(Class,5);// [...]// Create array of grades, using the 3 classes above for the classId and making one for each student.constgrades=ctx.using({'classId':classes}).cross({'studentId':students}).create(Grade);// [...]constisStudent=students[0]instanceofStudent;// true// Get an object with all the instances created...constallData=ctx.data();// { "Student": [...], "Teacher": [...], etc. }constallJson=ctx.json(true);// Formatted// '{ "Student": [...], "Teacher": [...], etc. }'// Pipe the data somewhere!ctx.stream().pipe(myDbInserter);// { "type": "Student", "value": { ... }}// Pipe the JSON somewhere!ctx.stream().pipe(myFileInserter);// (same as above but 1 per line)

CLI

// Docs coming soon! (see ~/examples/stuffd.typescript.ts for example)
stuffd run default --seed 2319stuffd create Spaceship 2> ships.json

Contribute

  1. Fork it
  2. npm i
  3. npm run watch
  4. Make changes andwrite tests.
  5. Send pull request! 😎

License:

MIT


[8]ページ先頭

©2009-2025 Movatter.jp