CodeSampleX

サンプル

yaml 2.9.0: Manage YAML nested collections and nodes with getIn, setIn, and deleteIn

検証済みサンプル — npm yaml 2.9.0: Manage YAML nested collections and nodes with getIn, setIn, and deleteIn. node 22 · linux alpine/x64 · docker で contract…

sha256:c566bb475ef702e2740e64fcaa2081cd8483f721990ba0aae601af7c899ce0e4

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
1
ビルドした署名鍵
1
宣言された環境 node windows 11 x64 node javascript 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
ゴール
Manage YAML nested collections and nodes with getIn, setIn, and deleteIn
パッケージ
シンボル
  • Document
  • deleteIn
  • getIn
  • setIn
環境
node
作成日
2026-08-19T02:23:28Z

コントラクト

  1. Document getIn retrieves scalar values and nested AST collection nodes along a path
  2. Document setIn updates existing values and creates nested intermediate collection maps
  3. Document deleteIn removes keys or elements at nested paths and returns boolean status
  4. Document getIn and setIn support zero-indexed paths for sequence elements
  5. Document toString serializes AST modifications made via setIn and deleteIn while preserving comments

ファイル

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

ソースアーティファクトをダウンロード (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: Manage YAML nested collections and nodes with getIn, setIn, and deleteIn
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/yaml@2.9.0
Demonstrate these symbols/APIs:
  - Document
  - deleteIn
  - getIn
  - setIn
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:57bef6d696b68204675163aef685dce9a948825358dcf2b50606df055e55fe7f","constraints":{"executionContext":"node"},"contract":["Document getIn retrieves scalar values and nested AST collection nodes along a path","Document setIn updates existing values and creates nested intermediate collection maps","Document deleteIn removes keys or elements at nested paths and returns boolean status","Document getIn and setIn support zero-indexed paths for sequence elements","Document toString serializes AST modifications made via setIn and deleteIn while preserving comments"],"goal":"Manage YAML nested collections and nodes with getIn, setIn, and deleteIn","kind":"HOW","packages":["pkg:npm/yaml@2.9.0"],"schemaVersion":1,"symbols":["Document","deleteIn","getIn","setIn"]},"contractCommand":["node","test/contract.js"],"environment":{"arch":"x64","ecosystem":"npm","executionContext":"node","language":"javascript","moduleSystem":"cjs","os":"windows","osVersionBucket":"11","packageManager":"npm","runtime":"node","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/yaml@2.9.0"],"schemaVersion":1,"symbols":["Document","deleteIn","getIn","setIn"],"verifierAdapter":"node-typescript@1"}
index.js
const { Document, parseDocument } = require('yaml');

function createDocument(value) {
  return new Document(value);
}

function parseYaml(yamlString) {
  return parseDocument(yamlString);
}

function getValueIn(doc, path, keepScalar = false) {
  return doc.getIn(path, keepScalar);
}

function setValueIn(doc, path, value) {
  doc.setIn(path, value);
  return doc;
}

function removeValueIn(doc, path) {
  return doc.deleteIn(path);
}

module.exports = {
  Document,
  parseDocument,
  createDocument,
  parseYaml,
  getValueIn,
  setValueIn,
  removeValueIn
};
package-lock.json
{
  "name": "sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample",
      "version": "1.0.0",
      "dependencies": {
        "yaml": "2.9.0"
      }
    },
    "node_modules/yaml": {
      "version": "2.9.0",
      "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.9.0.tgz",
      "integrity": "sha512-2AvhNX3mb8zd6Zy7INTtSpl1F15HW6Wnqj0srWlkKLcpYl/gMIMJiyuGq2KeI2YFxUPjdlB+3Lc10seMLtL4cA==",
      "license": "ISC",
      "bin": {
        "yaml": "bin.mjs"
      },
      "engines": {
        "node": ">= 14.6"
      },
      "funding": {
        "url": "https://github.com/sponsors/eemeli"
      }
    }
  }
}
package.json
{
  "name": "sample",
  "version": "1.0.0",
  "private": true,
  "main": "index.js",
  "dependencies": {
    "yaml": "2.9.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "Manage YAML nested collections and nodes with getIn, setIn, and deleteIn",
  "kind": "HOW",
  "packages": [
    "pkg:npm/yaml@2.9.0"
  ],
  "symbols": [
    "Document",
    "deleteIn",
    "getIn",
    "setIn"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@11.8.0",
    "runtime": "node@24.13.1"
  }
}
test/contract.js
const assert = require('assert');
const {
  Document,
  parseDocument,
  createDocument,
  parseYaml,
  getValueIn,
  setValueIn,
  removeValueIn
} = require('../index.js');

// 1. Document getIn retrieves scalar values and nested AST collection nodes along a path
const yamlText1 = 'server:\n  host: 127.0.0.1\n  port: 8080\n';
const doc1 = parseYaml(yamlText1);
assert.strictEqual(doc1.getIn(['server', 'host']), '127.0.0.1');
assert.strictEqual(getValueIn(doc1, ['server', 'port']), 8080);
assert.strictEqual(doc1.getIn(['nonexistent', 'path']), undefined);

// 2. Document setIn updates existing values and creates nested intermediate collection maps
const doc2 = new Document({ title: 'Config' });
doc2.setIn(['database', 'primary', 'port'], 5432);
setValueIn(doc2, ['database', 'primary', 'user'], 'admin');
assert.strictEqual(doc2.getIn(['database', 'primary', 'port']), 5432);
assert.strictEqual(doc2.getIn(['database', 'primary', 'user']), 'admin');

// 3. Document deleteIn removes keys or elements at nested paths and returns boolean status
const yamlText3 = 'services:\n  auth:\n    enabled: true\n    timeout: 30\n';
const doc3 = parseDocument(yamlText3);
const deleted1 = removeValueIn(doc3, ['services', 'auth', 'timeout']);
assert.strictEqual(deleted1, true);
assert.strictEqual(doc3.getIn(['services', 'auth', 'timeout']), undefined);
assert.strictEqual(doc3.getIn(['services', 'auth', 'enabled']), true);
const deleted2 = doc3.deleteIn(['services', 'unknown']);
assert.strictEqual(deleted2, false);

// 4. Document getIn and setIn support zero-indexed paths for sequence elements
const yamlText4 = 'pipeline:\n  stages:\n    - build\n    - test\n    - deploy\n';
const doc4 = parseDocument(yamlText4);
assert.strictEqual(doc4.getIn(['pipeline', 'stages', 1]), 'test');
doc4.setIn(['pipeline', 'stages', 1], 'integration-test');
assert.strictEqual(doc4.getIn(['pipeline', 'stages', 1]), 'integration-test');

// 5. Document toString serializes AST modifications made via setIn and deleteIn while preserving comments
const yamlText5 = '# Application configuration\napp:\n  name: service-a # main name\n  debug: true\n';
const doc5 = parseDocument(yamlText5);
doc5.setIn(['app', 'debug'], false);
doc5.setIn(['app', 'version'], '1.0.0');
doc5.deleteIn(['app', 'name']);
const serialized = doc5.toString();
assert.ok(serialized.includes('# Application configuration'));
assert.ok(serialized.includes('debug: false'));
assert.ok(serialized.includes('version: 1.0.0'));
assert.ok(!serialized.includes('service-a'));

console.log('PASS: all contract assertions passed for Document, getIn, setIn, and deleteIn');

オリジンシーダー

匿名