randexp vs faker vs chance vs casual
JavaScript/TypeScriptにおけるダミーデータ生成ライブラリの比較
randexpfakerchancecasual類似パッケージ:

JavaScript/TypeScriptにおけるダミーデータ生成ライブラリの比較

casualchancefakerrandexp はいずれもJavaScript/TypeScript環境でダミーデータやテストデータを生成するためのnpmパッケージです。casualchanceはシンプルなAPIで基本的な擬似データ(名前、メール、住所など)を提供しますが、現在は非推奨またはメンテナンスモードに入っています。faker(正式には@faker-js/faker)は多言語対応、豊富なデータカテゴリ、TypeScriptサポートを備えた現代的な選択肢です。一方、randexpは正規表現パターンに基づいて文字列を生成する特殊な用途に特化したライブラリです。これらは共通の目的を持ちつつも、設計思想、機能範囲、メンテナンス状況において明確な違いがあります。

npmのダウンロードトレンド

3 年

GitHub Starsランキング

統計詳細

パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
randexp6,925,4691,871-148年前MIT
faker2,298,380-10.1 MB--MIT
chance2,139,0476,5572.13 MB1779ヶ月前MIT
casual293,4393,017-417年前MIT

データ生成ライブラリ比較:casual vs chance vs faker vs randexp

フロントエンド開発において、テストデータやダミー入力を生成する必要は頻繁にあります。casualchancefakerrandexp はそれぞれ異なるアプローチでこの課題に取り組んでいますが、設計思想や機能範囲には明確な違いがあります。実際のコードを交えながら、各ライブラリの特性と使いどころを詳しく見ていきましょう。

🧪 基本的な使い方とAPI設計

casual はグローバル関数として直接呼び出せるシンプルな設計です。

// casual
const casual = require('casual');

console.log(casual.username); // 'john_doe'
console.log(casual.email);    // 'alice@example.com'

chance も同様にインスタンスを作らずに利用できますが、コンストラクタ経由での初期化も可能です。

// chance
const Chance = require('chance');
const chance = new Chance();

console.log(chance.name());   // 'Sarah Johnson'
console.log(chance.email());  // 'michael92@gmail.com'

faker はモジュール全体をインポートし、名前空間で機能を分類しています。

// faker
const { faker } = require('@faker-js/faker');

console.log(faker.internet.userName()); // 'CoolUser123'
console.log(faker.internet.email());    // 'john.doe@example.net'

randexp は正規表現から文字列を生成するという特殊な用途に特化しています。

// randexp
const RandExp = require('randexp');

const reg = new RandExp(/\d{3}-\d{2}-\d{4}/);
console.log(reg.gen()); // '123-45-6789'

🌍 国際化(ローカライズ)対応

casual は日本語を含む複数の言語に対応していますが、デフォルトでは英語です。

// casual - 日本語設定
const casual = require('casual');
casual.locale = 'ja';

console.log(casual.address); // '東京都渋谷区道玄坂1-2-3'

chance は国際化サポートが限定的で、主に英語ベースのデータを提供します。

// chance - 国際化サポートなし
const chance = new Chance();
console.log(chance.name()); // 常に英語圏の名前

faker は多言語対応が非常に充実しており、日本語を含む50以上のロケールをサポートしています。

// faker - 日本語設定
const { fakerJA } = require('@faker-js/faker');

console.log(fakerJA.location.streetAddress()); // '大阪府大阪市北区梅田2-4-9'

randexp は言語に依存しないため、国際化とは無関係です。

🔢 数値・日付生成の柔軟性

casual は整数や浮動小数点数の生成に加え、日付も簡単に扱えます。

// casual - 数値と日付
casual.integer(1, 100);        // 1〜100の整数
casual.double(0, 10);          // 0〜10の小数
casual.date('YYYY/MM/DD');     // '2023/05/17'

chance も同様に幅広い数値生成オプションを提供します。

// chance - 数値と日付
chance.integer({ min: 1, max: 100 });
chance.floating({ min: 0, max: 10 });
chance.date({ string: true, american: false }); // '2023-05-17T00:00:00.000Z'

faker は数値生成に特化したメソッドを持ち、日付操作も柔軟です。

// faker - 数値と日付
faker.number.int({ min: 1, max: 100 });
faker.number.float({ min: 0, max: 10 });
faker.date.past(); // 過去の日時

randexp は数値生成に特化しておらず、正規表現パターンによる文字列生成のみです。

📝 カスタムジェネレーターの拡張性

casual は独自のジェネレーターを定義できます。

// casual - カスタムジェネレーター
casual.define('pet_name', function() {
  return casual.random_element(['ポチ', 'タマ', 'ハチ']);
});

console.log(casual.pet_name); // 'タマ'

chance もカスタム関数の追加が可能です。

// chance - カスタムジェネレーター
chance.mixin({
  petName: function() {
    return this.pick(['ポチ', 'タマ', 'ハチ']);
  }
});

console.log(chance.petName()); // 'ハチ'

fakerdefine メソッドで新しいジェネレーターを追加できます。

// faker - カスタムジェネレーター
faker.define({
  key: 'petName',
  entries: [{ name: 'ポチ' }, { name: 'タマ' }, { name: 'ハチ' }],
  method: (entries) => faker.helpers.arrayElement(entries).name,
});

console.log(faker.petName()); // 'ポチ'

randexp は拡張性よりも特定の正規表現パターンへの対応に特化しているため、カスタムジェネレーターの概念はありません。

⚠️ メンテナンス状況と今後の使用について

重要な注意点として、casualchance は公式に非推奨(deprecated)となっており、新規プロジェクトでの使用は避けるべきです。

  • casual の npm ページには「This package is deprecated. Please use @faker-js/faker instead」と明記されています。
  • chance も GitHub リポジトリで「Chance is in maintenance mode. No new features will be added」と宣言されており、積極的な開発は行われていません。

一方、faker(正式名称は @faker-js/faker)は現在も活発にメンテナンスされており、TypeScript サポートやモダンな API 設計が特徴です。

randexp はニッチな用途に特化したライブラリであり、その目的に対しては安定して利用可能です。

🧩 実用的なユースケース別選定ガイド

テストデータ生成(包括的)

WebアプリのE2Eテストでユーザー情報、住所、電話番号などを一括生成したい場合:

  • faker — 多言語対応、豊富なカテゴリ、アクティブなメンテナンス
const user = {
  name: fakerJA.person.fullName(),
  email: fakerJA.internet.email(),
  phone: fakerJA.phone.number(),
  address: fakerJA.location.streetAddress()
};

正規表現ベースの文字列生成

パスワード、ID、シリアル番号など、特定のパターンに従った文字列が必要な場合:

  • randexp — 正規表現から文字列を逆生成できる唯一の選択肢
const passwordPattern = new RandExp(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$/);
console.log(passwordPattern.gen()); // 'A1b2C3d4'

単純なランダム値(レガシー環境)

既存システムで casualchance が使われており、マイグレーションの余裕がない場合:

  • ⚠️ casual / chance — 新規プロジェクトでは使用しないこと

📊 まとめ:各ライブラリの位置付け

ライブラリ状態主な用途推奨度
fakerアクティブ包括的なテストデータ生成✅✅✅ 高
randexp安定正規表現ベースの文字列生成✅✅ 中(用途限定)
casual非推奨レガシーデータ生成❌ 低
chanceメンテナンスモードレガシーデータ生成❌ 低

💡 最終的なアドバイス

  • 新規プロジェクトでは @faker-js/faker を使うべきです。多言語対応、TypeScript サポート、モダンなAPI、活発なコミュニティが大きな強みです。
  • 正規表現から文字列を生成したい場合は randexp が最適です。他のライブラリでは代替できません。
  • casualchance は既存コードの保守以外で使うべきではありません。セキュリティアップデートやバグ修正が保証されないためです。

これらのライブラリは似た目的を持ちますが、実際の使い勝手や将来性には大きな差があります。プロジェクトの要件と長期的なメンテナンス性を考慮して、賢く選んでください。

選び方: randexp vs faker vs chance vs casual

  • randexp:

    randexpは正規表現パターンに基づいて文字列を生成する特殊な用途に特化したライブラリです。パスワード、ID、シリアル番号など特定の形式を持つ文字列を生成したい場合にのみ使用してください。一般的なダミーデータ生成には向いていませんが、そのニッチな領域では他に代替がありません。

  • faker:

    faker@faker-js/faker)は現在最も推奨されるダミーデータ生成ライブラリです。日本語を含む50以上のロケール対応、TypeScriptサポート、モダンなAPI設計、活発なコミュニティが強みです。包括的なテストデータ生成が必要なフロントエンド・バックエンド両方のプロジェクトに最適です。

  • chance:

    chanceは現在メンテナンスモードに入っており、新機能の追加は行われていません。シンプルなランダムデータ生成が必要なレガシーシステムでは引き続き使えますが、新規プロジェクトではよりアクティブに開発されているfakerを推奨します。国際化サポートが弱い点にも注意が必要です。

  • casual:

    casualは非推奨(deprecated)のライブラリであり、新規プロジェクトでの使用は避けるべきです。既存システムで使われている場合にのみ保守目的で利用し、可能であれば@faker-js/fakerへの移行を検討してください。APIはシンプルで直感的ですが、セキュリティやバグ修正の保証がありません。

randexp のREADME

randexp.js

randexp will generate a random string that matches a given RegExp Javascript object.

Build Status Dependency Status codecov

Usage

const RandExp = require('randexp');

// supports grouping and piping
new RandExp(/hello+ (world|to you)/).gen();
// => hellooooooooooooooooooo world

// sets and ranges and references
new RandExp(/<([a-z]\w{0,20})>foo<\1>/).gen();
// => <m5xhdg>foo<m5xhdg>

// wildcard
new RandExp(/random stuff: .+/).gen();
// => random stuff: l3m;Hf9XYbI [YPaxV>U*4-_F!WXQh9>;rH3i l!8.zoh?[utt1OWFQrE ^~8zEQm]~tK

// ignore case
new RandExp(/xxx xtreme dragon warrior xxx/i).gen();
// => xxx xtReME dRAGON warRiOR xXX

// dynamic regexp shortcut
new RandExp('(sun|mon|tue|wednes|thurs|fri|satur)day', 'i');
// is the same as
new RandExp(new RegExp('(sun|mon|tue|wednes|thurs|fri|satur)day', 'i'));

If you're only going to use gen() once with a regexp and want slightly shorter syntax for it

const randexp = require('randexp').randexp;

randexp(/[1-6]/); // 4
randexp('great|good( job)?|excellent'); // great

If you miss the old syntax

require('randexp').sugar();

/yes|no|maybe|i don't know/.gen(); // maybe

Motivation

Regular expressions are used in every language, every programmer is familiar with them. Regex can be used to easily express complex strings. What better way to generate a random string than with a language you can use to express the string you want?

Thanks to String-Random for giving me the idea to make this in the first place and randexp for the sweet .gen() syntax.

Default Range

The default generated character range includes printable ASCII. In order to add or remove characters, a defaultRange attribute is exposed. you can subtract(from, to) and add(from, to)

const randexp = new RandExp(/random stuff: .+/);
randexp.defaultRange.subtract(32, 126);
randexp.defaultRange.add(0, 65535);
randexp.gen();
// => random stuff: 湐箻ໜ䫴␩⶛㳸長���邓蕲뤀쑡篷皇硬剈궦佔칗븛뀃匫鴔事좍ﯣ⭼ꝏ䭍詳蒂䥂뽭

You can also change the default range by changing RandExp.prototype.defaultRange.

Custom PRNG

The default randomness is provided by Math.random(). If you need to use a seedable or cryptographic PRNG, you can override RandExp.prototype.randInt or randexp.randInt (where randexp is an instance of RandExp). randInt(from, to) accepts an inclusive range and returns a randomly selected number within that range.

Infinite Repetitionals

Repetitional tokens such as *, +, and {3,} have an infinite max range. In this case, randexp looks at its min and adds 100 to it to get a useable max value. If you want to use another int other than 100 you can change the max property in RandExp.prototype or the RandExp instance.

const randexp = new RandExp(/no{1,}/);
randexp.max = 1000000;

With RandExp.sugar()

const regexp = /(hi)*/;
regexp.max = 1000000;

Bad Regular Expressions

There are some regular expressions which can never match any string.

  • Ones with badly placed positionals such as /a^/ and /$c/m. Randexp will ignore positional tokens.

  • Back references to non-existing groups like /(a)\1\2/. Randexp will ignore those references, returning an empty string for them. If the group exists only after the reference is used such as in /\1 (hey)/, it will too be ignored.

  • Custom negated character sets with two sets inside that cancel each other out. Example: /[^\w\W]/. If you give this to randexp, it will return an empty string for this set since it can't match anything.

Projects based on randexp.js

JSON-Schema Faker

Use generators to populate JSON Schema samples. See: jsf on github and jsf demo page.

Install

Node.js

npm install randexp

Browser

Download the minified version from the latest release.

Tests

Tests are written with mocha

npm test

Integration with TypeScript

RandExp includes TypeScript definitions.

import * as RandExp from "randexp";
const randexp = new RandExp(/[a-z]{6}/);
randexp.gen();

Use dtslint to check the definition file.

npm install -g dtslint
npm run dtslint