CodeSampleX

Sample

p-locate 5.0.0: p-locate

Verified sample for npm p-locate 5.0.0: p-locate. The contract ran on node 22 · linux alpine/x64 · docker and passed: p-locate returns the first element in…

sha256:e40b69446b33207ee5bff9f6cde6a10f679db596f8eff8e2fb6b95fed7b0588d

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
1
Signing keys that built it
1
Declared environment node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Verification-run environments

Environment Contract Stages Run
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-08-27

Case

HOW
Goal
verify p-locate in pkg:npm/p-locate@5.0.0
Packages
Symbols
  • p-locate
Environment
node 22.23.2
Created
2026-08-27T10:02:53Z

Contract

  1. p-locate returns the first element in iterable order satisfying an async predicate
  2. p-locate returns the first element satisfying a synchronous predicate
  3. p-locate accepts an iterable of promises and awaits each element before testing
  4. p-locate preserves input order by default when searching across async predicates with variable resolution times
  5. p-locate returns the fastest resolved match when preserveOrder is set to false
  6. p-locate respects the concurrency option to limit concurrent test executions
  7. p-locate resolves to undefined when no elements satisfy the predicate
  8. p-locate propagates errors when a predicate rejects

Files

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.js
  • test/contract.js

Download the source artifact (tar.gz)

Source

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify p-locate in pkg:npm/p-locate@5.0.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/p-locate@5.0.0
Demonstrate these symbols/APIs:
  - p-locate

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:d101a3b33e4004fa25c779aa59797c09ea01c4aa62fb933241e74a898bfd8ee3","contract":["p-locate returns the first element in iterable order satisfying an async predicate","p-locate returns the first element satisfying a synchronous predicate","p-locate accepts an iterable of promises and awaits each element before testing","p-locate preserves input order by default when searching across async predicates with variable resolution times","p-locate returns the fastest resolved match when preserveOrder is set to false","p-locate respects the concurrency option to limit concurrent test executions","p-locate resolves to undefined when no elements satisfy the predicate","p-locate propagates errors when a predicate rejects"],"goal":"verify p-locate in pkg:npm/p-locate@5.0.0","kind":"HOW","packages":["pkg:npm/p-locate@5.0.0"],"schemaVersion":1,"symbols":["p-locate"]},"contractCommand":["node","test/contract.js"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/p-locate@5.0.0"],"schemaVersion":1,"subject":"pkg:npm/p-locate@5.0.0","symbols":["p-locate"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "p-locate-sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "p-locate-sample",
      "version": "1.0.0",
      "dependencies": {
        "p-locate": "5.0.0"
      }
    },
    "node_modules/p-limit": {
      "version": "3.1.0",
      "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-3.1.0.tgz",
      "integrity": "sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ==",
      "license": "MIT",
      "dependencies": {
        "yocto-queue": "^0.1.0"
      },
      "engines": {
        "node": ">=10"
      },
      "funding": {
        "url": "https://github.com/sponsors/sindresorhus"
      }
    },
    "node_modules/p-locate": {
      "version": "5.0.0",
      "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-5.0.0.tgz",
      "integrity": "sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw==",
      "license": "MIT",
      "dependencies": {
        "p-limit": "^3.0.2"
      },
      "engines": {
        "node": ">=10"
      },
      "funding": {
        "url": "https://github.com/sponsors/sindresorhus"
      }
    },
    "node_modules/yocto-queue": {
      "version": "0.1.0",
      "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz",
      "integrity": "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==",
      "license": "MIT",
      "engines": {
        "node": ">=10"
      },
      "funding": {
        "url": "https://github.com/sponsors/sindresorhus"
      }
    }
  }
}
package.json
{
  "name": "p-locate-sample",
  "version": "1.0.0",
  "dependencies": {
    "p-locate": "5.0.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify p-locate in pkg:npm/p-locate@5.0.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/p-locate@5.0.0"
  ],
  "symbols": [
    "p-locate"
  ]
}
src/index.js
'use strict';

const pLocate = require('p-locate');

/**
 * Finds the first element in an iterable that satisfies an asynchronous or synchronous tester.
 *
 * @template T
 * @param {Iterable<T | Promise<T>>} iterable - An iterable of values or promises.
 * @param {(element: T) => (boolean | Promise<boolean>)} tester - Predicate function returning boolean or Promise<boolean>.
 * @param {object} [options] - Options for pLocate.
 * @param {number} [options.concurrency] - Concurrency limit for tester executions.
 * @param {boolean} [options.preserveOrder] - Whether to preserve input order.
 * @returns {Promise<T | undefined>} The first matching element, or undefined.
 */
async function findFirst(iterable, tester, options) {
  return pLocate(iterable, tester, options);
}

/**
 * Finds the first element with a concurrency limit.
 *
 * @template T
 * @param {Iterable<T | Promise<T>>} iterable - An iterable of values or promises.
 * @param {(element: T) => (boolean | Promise<boolean>)} tester - Predicate function.
 * @param {number} concurrency - Max concurrent tests.
 * @returns {Promise<T | undefined>} The first matching element.
 */
async function findWithConcurrency(iterable, tester, concurrency) {
  return pLocate(iterable, tester, { concurrency });
}

/**
 * Finds the fastest resolving matching element without preserving input order.
 *
 * @template T
 * @param {Iterable<T | Promise<T>>} iterable - An iterable of values or promises.
 * @param {(element: T) => (boolean | Promise<boolean>)} tester - Predicate function.
 * @returns {Promise<T | undefined>} The fastest resolved matching element.
 */
async function findFastestMatch(iterable, tester) {
  return pLocate(iterable, tester, { preserveOrder: false });
}

module.exports = {
  findFirst,
  findWithConcurrency,
  findFastestMatch,
};
test/contract.js
'use strict';

const assert = require('node:assert/strict');
const {
  findFirst,
  findWithConcurrency,
  findFastestMatch,
} = require('../src/index.js');

const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

async function main() {
  // Contract 1: p-locate returns the first element in iterable order satisfying an async predicate
  {
    const items = ['apple', 'banana', 'blueberry', 'cherry'];
    const result = await findFirst(items, async (item) => {
      await delay(5);
      return item.startsWith('b');
    });
    assert.strictEqual(result, 'banana');
  }

  // Contract 2: p-locate returns the first element satisfying a synchronous predicate
  {
    const numbers = [10, 25, 30, 45];
    const result = await findFirst(numbers, (num) => num > 20);
    assert.strictEqual(result, 25);
  }

  // Contract 3: p-locate accepts an iterable of promises and awaits each element before testing
  {
    const promiseItems = [
      Promise.resolve('alpha'),
      delay(10).then(() => 'beta'),
      Promise.resolve('gamma'),
    ];
    const result = await findFirst(promiseItems, async (item) => item === 'beta');
    assert.strictEqual(result, 'beta');
  }

  // Contract 4: p-locate preserves input order by default when searching across async predicates with variable resolution times
  {
    const items = ['slow-first', 'fast-second'];
    const result = await findFirst(items, async (item) => {
      if (item === 'slow-first') {
        await delay(50);
        return true;
      }
      if (item === 'fast-second') {
        await delay(5);
        return true;
      }
      return false;
    });
    assert.strictEqual(result, 'slow-first');
  }

  // Contract 5: p-locate returns the fastest resolved match when preserveOrder is set to false
  {
    const items = ['slow-match', 'fast-match'];
    const result = await findFastestMatch(items, async (item) => {
      if (item === 'slow-match') {
        await delay(60);
        return true;
      }
      if (item === 'fast-match') {
        await delay(10);
        return true;
      }
      return false;
    });
    assert.strictEqual(result, 'fast-match');
  }

  // Contract 6: p-locate respects the concurrency option to limit concurrent test executions
  {
    const items = [1, 2, 3, 4, 5, 6];
    let activeTests = 0;
    let maxConcurrent = 0;

    const result = await findWithConcurrency(
      items,
      async (num) => {
        activeTests++;
        maxConcurrent = Math.max(maxConcurrent, activeTests);
        await delay(15);
        activeTests--;
        return num === 5;
      },
      2
    );

    assert.strictEqual(result, 5);
    assert.strictEqual(maxConcurrent <= 2, true);
    assert.strictEqual(maxConcurrent >= 1, true);
  }

  // Contract 7: p-locate resolves to undefined when no elements satisfy the predicate
  {
    const items = ['one', 'two', 'three'];
    const result = await findFirst(items, (item) => item === 'four');
    assert.strictEqual(result, undefined);

    const emptyResult = await findFirst([], () => true);
    assert.strictEqual(emptyResult, undefined);
  }

  // Contract 8: p-locate propagates errors when a predicate rejects
  {
    const items = ['valid', 'error-trigger'];
    let caughtError = null;

    try {
      await findFirst(items, async (item) => {
        if (item === 'error-trigger') {
          throw new Error('Predicate failed execution');
        }
        return false;
      });
    } catch (err) {
      caughtError = err;
    }

    assert.notStrictEqual(caughtError, null);
    assert.strictEqual(caughtError.message, 'Predicate failed execution');
  }

  console.log('All p-locate contracts verified successfully.');
}

main().catch((err) => {
  console.error('Contract test failed:', err);
  process.exit(1);
});

Origin Seeder

anonymous