ipld-garbage
Version:
Garbage data generator for the IPLD Data Model
59 lines (54 loc) • 1.41 kB
JavaScript
var ipldGarbage = require('./ipld-garbage.js');
var is = require('@sindresorhus/is');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var is__default = /*#__PURE__*/_interopDefaultLegacy(is);
const expectedTypes = [
'null',
'boolean',
'int',
'float',
'string',
'Uint8Array',
'Array',
'Object',
'CID'
];
const types = {};
const account = obj => {
let type = is__default["default"](obj);
if (type === 'Object' && obj.asCID === obj) {
type = 'CID';
} else if (type === 'number') {
type = Number.isInteger(obj) ? 'int' : 'float';
}
types[type] = (types[type] || 0) + 1;
return type;
};
for (let i = 0; i < 1000; i++) {
const obj = ipldGarbage.garbage();
const type = account(obj);
if (type === 'Object') {
for (const value of Object.values(obj)) {
account(value);
}
} else if (type === 'Array') {
for (const value of obj) {
account(value);
}
}
}
for (const type of expectedTypes) {
if (!types[type]) {
throw new Error(`Did not generate any ${ type } values`);
}
if (types[type] < 20) {
throw new Error(`Did not generate enough ${ type } values`);
}
}
for (const type of Object.keys(types)) {
if (!expectedTypes.includes(type)) {
throw new Error(`Got unexpected type ${ type }`);
}
}
console.log('\x1B[32m\u2714\x1B[39m yep');
;