JavaScript 中的随机对象生成器

发布于 2024-08-25 15:34:10 字数 139 浏览 15 评论 0原文

我需要一个 JavaScript 中的随机对象生成器,它可以生成具有不同字段和值的各种对象。我有什么想法可以找到这样的工具吗?

我需要生成具有各种复杂性的随机对象。我的目标是使用 JSON 来序列化这些对象并模糊测试我的应用程序 http api。

I need a random object generator in JavaScript that generates a variety of objects with different fields and values. Any ideas where I can find such tool?

I need to generate random objects with various complexity.. My goal is to use JSON in order to serialize these objects and fuzz test my application http api.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扎心 2024-09-01 15:34:10
function createRandomObj(fieldCount, allowNested)
{
    var generatedObj = {};

    for(var i = 0; i < fieldCount; i++) {
        var generatedObjField;

        switch(randomInt(allowNested ? 6 : 5)) {

            case 0:
            generatedObjField = randomInt(1000);
            break;

            case 1:
            generatedObjField = Math.random();
            break;

            case 2:
            generatedObjField = Math.random() < 0.5 ? true : false;
            break;

            case 3:
            generatedObjField = randomString(randomInt(4) + 4);
            break;

            case 4:
            generatedObjField = null;
            break;

            case 5:
            generatedObjField = createRandomObj(fieldCount, allowNested);
            break;
        }
        generatedObj[randomString(8)] = generatedObjField;
    }
    return generatedObj;
}

// helper functions

function randomInt(rightBound)
{
    return Math.floor(Math.random() * rightBound);
}

function randomString(size)
{
    var alphaChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var generatedString = '';
    for(var i = 0; i < size; i++) {
        generatedString += alphaChars[randomInt(alphaChars.length)];
    }

    return generatedString;
}

它将创建一个带有 X 个参数的 obj,所有参数都带有整数、浮点数、字符串、布尔值或空值。
我刚刚做到了:B

function createRandomObj(fieldCount, allowNested)
{
    var generatedObj = {};

    for(var i = 0; i < fieldCount; i++) {
        var generatedObjField;

        switch(randomInt(allowNested ? 6 : 5)) {

            case 0:
            generatedObjField = randomInt(1000);
            break;

            case 1:
            generatedObjField = Math.random();
            break;

            case 2:
            generatedObjField = Math.random() < 0.5 ? true : false;
            break;

            case 3:
            generatedObjField = randomString(randomInt(4) + 4);
            break;

            case 4:
            generatedObjField = null;
            break;

            case 5:
            generatedObjField = createRandomObj(fieldCount, allowNested);
            break;
        }
        generatedObj[randomString(8)] = generatedObjField;
    }
    return generatedObj;
}

// helper functions

function randomInt(rightBound)
{
    return Math.floor(Math.random() * rightBound);
}

function randomString(size)
{
    var alphaChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var generatedString = '';
    for(var i = 0; i < size; i++) {
        generatedString += alphaChars[randomInt(alphaChars.length)];
    }

    return generatedString;
}

It will create a obj with X paramenters, all with a integer, float, string, boolean or null value.
I just made it :B

高速公鹿 2024-09-01 15:34:10

您可以使用 hasard

JavaScript 中的随机变量和随机嵌套对象操作

const h = require('hasard');

const randomInteger = h.integer({type: 'poisson', lambda: 4});

const randomString = h.string({
    size: h.add(randomInteger, 5),
    value: h.value('abcdefghijklmnopkrstuvw'.split(''))
});

const randomNumber = h.number([0, 100]);

const randomKeys = h.array({
    size: randomInteger,
    value: randomString
});

// we first define it, to use it as reference into randomObject
const randomValue = h.value();

const randomObject = h.object(
    randomKeys,
    randomValue
);

// And we set recursivity by setting his values afterward
randomValue.set([
    randomString,
    randomObject,
    randomNumber,
    randomInteger
]);

结果看起来像

[
  {
    "vbmujvv": "rfigcpcvpj",
    "sjmcgvvk": 3,
    "efdarehl": {
      "odinthsuca": "rwjhmbfus",
      "noihjtjen": 27.73332043042913,
      "brspkaagb": "lnuiabcfd"
    },
    "febtungjhfokf": 49.28625818957401,
    "eoemrkgi": {
      "jkcuwrpsh": "ekjoltm",
      "cincs": {
        "fcovbwk": {
          "whsgmjh": 48.00843935524626,
          "agsjflef": 46.700796253998014
        },
        "ovkdfudgfm": 84.83383163217746,
        "fpfetl": "djuvfjbjptf",
        "kobmkstj": {
          "wskgkkerk": 9,
          "kvnptptek": 37.63655947554132,
          "dsloun": 4
        }
      },
      "krirwk": {
        "sjgftomu": 51.663884142674775,
        "hpjgibnli": 4
      },
      "pkhkgruls": "isuodwjrg"
    },
    "ortomnue": 71.71303423929236
  }
]

免责声明 我是 hasard

You can use hasard library

Random variables and random nested objects manipulation in javascript

const h = require('hasard');

const randomInteger = h.integer({type: 'poisson', lambda: 4});

const randomString = h.string({
    size: h.add(randomInteger, 5),
    value: h.value('abcdefghijklmnopkrstuvw'.split(''))
});

const randomNumber = h.number([0, 100]);

const randomKeys = h.array({
    size: randomInteger,
    value: randomString
});

// we first define it, to use it as reference into randomObject
const randomValue = h.value();

const randomObject = h.object(
    randomKeys,
    randomValue
);

// And we set recursivity by setting his values afterward
randomValue.set([
    randomString,
    randomObject,
    randomNumber,
    randomInteger
]);

Results will looks like

[
  {
    "vbmujvv": "rfigcpcvpj",
    "sjmcgvvk": 3,
    "efdarehl": {
      "odinthsuca": "rwjhmbfus",
      "noihjtjen": 27.73332043042913,
      "brspkaagb": "lnuiabcfd"
    },
    "febtungjhfokf": 49.28625818957401,
    "eoemrkgi": {
      "jkcuwrpsh": "ekjoltm",
      "cincs": {
        "fcovbwk": {
          "whsgmjh": 48.00843935524626,
          "agsjflef": 46.700796253998014
        },
        "ovkdfudgfm": 84.83383163217746,
        "fpfetl": "djuvfjbjptf",
        "kobmkstj": {
          "wskgkkerk": 9,
          "kvnptptek": 37.63655947554132,
          "dsloun": 4
        }
      },
      "krirwk": {
        "sjgftomu": 51.663884142674775,
        "hpjgibnli": 4
      },
      "pkhkgruls": "isuodwjrg"
    },
    "ortomnue": 71.71303423929236
  }
]

DISCLAIMER i'm the author of hasard library

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文