OpenViSTAI · Agents · Blockchain · Crypto
Menu
OPENVIST RECIPES

Sort numeric version cores with JavaScript

JavaScript

Sort bounded major.minor.patch version cores numerically without claiming full semantic-version support.

JavaScript code excerpt: Sort numeric version cores with JavaScript. Version 0.1.0. Read the full recipe for code, sources and recorded test scope.

Sort bounded major.minor.patch version cores numerically without claiming full semantic-version support. This is a local data check under the documented conventions; it does not execute tools or contact a service.

Before you start

  • Save the code as recipe.cjs and run node recipe.cjs using Node.js 22. Supply the JSON array through stdin, then end stdin. No command-line arguments are accepted, including help options.
  • Example stdin: ["2.0.0","1.10.0","1.2.0"]. Output: {"versions":["1.2.0","1.10.0","2.0.0"]}.
  • Input must be strict UTF-8 JSON. The decoder preserves a leading BOM so JSON.parse rejects it. Native JSON.parse semantics apply, including ordinary JSON whitespace and string escape decoding.
  • The parsed input must be an array of at most 100 ASCII version strings. Each decoded string must contain exactly three dot-separated decimal components, each from 0 through 999999, without leading zeros except for 0 itself.
  • Versions are compared numerically by major, then minor, then patch. Original decoded strings and duplicates are preserved. An empty array produces {"versions":[]}.
  • Successful execution exits 0 naturally, writes exactly one JSON object followed by a newline to stdout, and writes nothing to stderr. Invalid arguments, invalid input, or stdin read failures exit 2 with no stdout and the generic lowercase stderr diagnostic error: invalid input.

Build and check

  1. Use Node.js 22 CommonJS. Save the code as recipe.cjs.
  2. Save the supplied JSON example as input.json, encoded as UTF-8 without a BOM.
  3. Run: node recipe.cjs < input.json
  4. Compare the output with the expected result; handle exit code 2 as invalid input.

Limits to understand

  • Only node:fs is required. Input is read exclusively from stdin using bounded readSync calls; no input paths are opened, files written, external commands executed, or network operations performed.
  • The program reads at most 65537 bytes and rejects input exceeding 65536 bytes. It may block waiting for stdin data or EOF.
  • Input is held in memory: a 65537-byte buffer, decoded text, the parsed JSON value, sorting records, and serialized output. The 100-record limit applies after parsing; there is no streaming JSON parser.
  • This sorts bounded numeric version cores only, not full semantic versions. Prerelease identifiers, build identifiers, signs, embedded whitespace, version ranges, and package resolution are unsupported.
  • Successful output repeats the supplied version strings and may therefore contain sensitive input data. Error diagnostics do not disclose input values.
  • Output-write failures are outside the exit-status and diagnostic guarantees.

Run it

node recipe.cjs < input.json

Example input

["1.10.0", "2.0.0", "1.2.3"]

Expected output

{"versions": ["1.2.3", "1.10.0", "2.0.0"]}
INSPECT THE SCRIPT

Inspect, copy and run the documented checks.

This browser preview builds code locally. It does not contact an API or run the code.

Default example ready.

JavaScript example
'use strict';

const fs = require('node:fs');

function main() {
  if (process.argv.length !== 2) {
    throw new Error();
  }

  const bytes = Buffer.alloc(65537);
  let used = 0;
  while (used < bytes.length) {
    const count = fs.readSync(0, bytes, used, bytes.length - used, null);
    if (count === 0) break;
    used += count;
  }
  if (used > 65536) {
    throw new Error();
  }

  const decoder = new TextDecoder('utf-8', {
    fatal: true,
    ignoreBOM: true
  });
  const text = decoder.decode(bytes.subarray(0, used));
  const input = JSON.parse(text);
  if (!Array.isArray(input) || input.length > 100) {
    throw new Error();
  }

  const corePattern = /^(0|[1-9][0-9]{0,5})\.(0|[1-9][0-9]{0,5})\.(0|[1-9][0-9]{0,5})$/;
  const records = [];
  for (const original of input) {
    if (typeof original !== 'string') {
      throw new Error();
    }
    const match = corePattern.exec(original);
    // JavaScript's $ can match before a final newline; require a full match.
    if (match === null || match[0] !== original) {
      throw new Error();
    }
    const major = Number(match[1]);
    const minor = Number(match[2]);
    const patch = Number(match[3]);
    for (const component of [major, minor, patch]) {
      if (!Number.isSafeInteger(component) || component < 0 || component > 999999) {
        throw new Error();
      }
    }
    records.push({ original, major, minor, patch });
  }

  records.sort((a, b) =>
    a.major - b.major || a.minor - b.minor || a.patch - b.patch
  );
  return JSON.stringify({ versions: records.map(record => record.original) });
}

let output;
try {
  output = main();
} catch {
  process.stderr.write('error: invalid input\n', () => process.exit(2));
  return;
}

// Output writes are intentionally outside the input-error handling guarantee.
process.stdout.write(output + '\n');
What to verify after running
  • The supplied example must match the documented JSON output.
  • Error cases must exit 2 with no stdout. Review sensitive successful output before sharing it.

Request or format preview

Illustrative preview. No provider response, price or balance is fetched.

Change a setting to inspect the preview.

Official sources and related profiles

    Code license: MIT. Keep the license notice with your copy.

    Example version 0.1.0 · Documentation reviewed 2026-10-14.

    Six independent CLI fixtures and 10 curated supplementary checks passed in the pinned offline Node.js 22 CommonJS container. This does not establish exhaustive correctness, production performance or compatibility with an external provider.

    Related browser tools

    Inspect related data locally in your browser. These tools have their own input rules and limits; they do not execute this recipe.