CodeSampleX

示例

function-bind 1.1.2: bind a function to a context with partial arguments using function-bind

已验证示例 — npm function-bind 1.1.2: bind a function to a context with partial arguments using function-bind. contract 在 node 22 · linux alpine/x64 · docker…

sha256:bd8e8235756ac8a10da9988c9736be28ff6032e6a597ed0987566ebc912a6be0

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 windows 11 x64 npm

验证运行环境

环境 契约 阶段 运行日期
node 22 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-19

案例

HOW
目标
bind a function to a context with partial arguments using function-bind
包
符号
  • function-bind
创建时间
2026-08-18T18:02:20Z

契约

  1. bind binds context and prepends arguments to function calls
  2. bind correctly calculates the length of the bound function
  3. bind creates a constructor that instantiates the target when called with new
  4. bind throws a TypeError when called on a non-function target

文件

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

下载源代码构件 (tar.gz)

源代码

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: bind a function to a context with partial arguments using function-bind
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/function-bind@1.1.2
Demonstrate these symbols/APIs:
  - function-bind
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@11.8.0
  - runtime: node@24.13.1

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:80b6ca41baa2684659d09f13e783f8c3dd8e1e4185eb011721dcc8d919609102","constraints":{"executionContext":"node"},"contract":["bind binds context and prepends arguments to function calls","bind correctly calculates the length of the bound function","bind creates a constructor that instantiates the target when called with new","bind throws a TypeError when called on a non-function target"],"goal":"bind a function to a context with partial arguments using function-bind","kind":"HOW","packages":["pkg:npm/function-bind@1.1.2"],"schemaVersion":1,"symbols":["function-bind"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","ecosystem":"npm","os":"windows","osVersionBucket":"11","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/function-bind@1.1.2"],"schemaVersion":1,"symbols":["function-bind"],"verifierAdapter":"node-typescript@1"}
index.mjs
import bind from 'function-bind';

/**
 * Binds a function to a context object with optional prepended arguments.
 * @param {Function} fn - The target function to bind.
 * @param {*} thisArg - The value to be passed as the this parameter to the target function.
 * @param {...*} args - Arguments to prepend to arguments provided to the bound function.
 * @returns {Function} A copy of the given function with the specified this value and initial arguments.
 */
export function bindFunction(fn, thisArg, ...args) {
  return bind.call(fn, thisArg, ...args);
}

export default bind;
package-lock.json
{
  "name": "sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample",
      "version": "1.0.0",
      "dependencies": {
        "function-bind": "1.1.2"
      }
    },
    "node_modules/function-bind": {
      "version": "1.1.2",
      "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz",
      "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==",
      "license": "MIT",
      "funding": {
        "url": "https://github.com/sponsors/ljharb"
      }
    }
  }
}
package.json
{
  "name": "sample",
  "version": "1.0.0",
  "type": "module",
  "dependencies": {
    "function-bind": "1.1.2"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "bind a function to a context with partial arguments using function-bind",
  "kind": "HOW",
  "packages": [
    "pkg:npm/function-bind@1.1.2"
  ],
  "symbols": [
    "function-bind"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@11.8.0",
    "runtime": "node@24.13.1"
  }
}
test/contract.mjs
import assert from 'node:assert/strict';
import bind from 'function-bind';

assert.equal(typeof bind, 'function', 'function-bind must be a function');

// Contract 1: bind binds context and prepends arguments to function calls
{
  function greet(greeting, punctuation) {
    return `${greeting}, ${this.name}${punctuation}`;
  }

  const context = { name: 'World' };
  const sayHello = bind.call(greet, context, 'Hello');

  assert.equal(sayHello('!'), 'Hello, World!');
  assert.equal(sayHello('?'), 'Hello, World?');

  function add(a, b, c) {
    return (this ? this.base : 0) + a + b + c;
  }
  const boundAdd = bind.call(add, { base: 10 }, 1, 2);
  assert.equal(boundAdd(3), 16);
}

// Contract 2: bind correctly calculates the length of the bound function
{
  function sum4(a, b, c, d) {
    return a + b + c + d;
  }

  const bound0 = bind.call(sum4, null);
  assert.equal(bound0.length, 4);

  const bound1 = bind.call(sum4, null, 1);
  assert.equal(bound1.length, 3);

  const bound3 = bind.call(sum4, null, 1, 2, 3);
  assert.equal(bound3.length, 1);

  const bound4 = bind.call(sum4, null, 1, 2, 3, 4);
  assert.equal(bound4.length, 0);

  const bound5 = bind.call(sum4, null, 1, 2, 3, 4, 5);
  assert.equal(bound5.length, 0);
}

// Contract 3: bind creates a constructor that instantiates the target when called with new
{
  function Point(x, y) {
    this.x = x;
    this.y = y;
  }

  Point.prototype.getDistance = function () {
    return Math.sqrt(this.x * this.x + this.y * this.y);
  };

  const YPoint = bind.call(Point, null, 0);
  const p = new YPoint(5);

  assert.equal(p instanceof Point, true);
  assert.equal(p.x, 0);
  assert.equal(p.y, 5);
  assert.equal(p.getDistance(), 5);
}

// Contract 4: bind throws a TypeError when called on a non-function target
{
  assert.throws(
    () => bind.call(null, {}),
    TypeError
  );

  assert.throws(
    () => bind.call(123, {}),
    TypeError
  );

  assert.throws(
    () => bind.call('string', {}),
    TypeError
  );
}

console.log('All contract assertions passed successfully.');

原始种子者

匿名