CodeSampleX

샘플

detect-newline 3.1.0: detect dominant newline character in string

검증된 샘플 — npm detect-newline 3.1.0: detect dominant newline character in string. node 22 · linux alpine/x64 · docker에서 contract를 실행해 통과했습니다.

sha256:9c081a5869dc085255db11112bea3a7f0fbb706a7aa48b4f2cb568f3b8c0e60f

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. MIT-0

실행 증거

선언된 환경과 서명된 실행을 분리해 두었습니다. 이 샘플이 무엇을 어디서 실행했는지 그대로 볼 수 있습니다.

증거 기준
서명된 컨트랙트 통과
검증 영수증
1
빌드한 서명 키
1
선언된 환경 linux 24 · ubuntu · glibc 2.39 x64 npm

검증 실행 환경

환경 컨트랙트 단계 실행일
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

케이스

HOW
목표
detect dominant newline character in string
패키지
심벌
  • detectNewline
  • detectNewlineGraceful
생성일
2026-08-27T18:52:28Z

컨트랙트

  1. detectNewline returns \r\n when CRLF is the dominant newline character
  2. detectNewline returns \n when LF is dominant or when LF and CRLF counts are equal
  3. detectNewline returns undefined when the string contains no newlines
  4. detectNewline throws TypeError when called with a non-string argument
  5. detectNewlineGraceful returns dominant newline character or defaults to \n when no newline is present
  6. detectNewlineGraceful returns \n gracefully for non-string inputs without throwing

파일

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.mjs
  • 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: detect dominant newline character in string
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/detect-newline@3.1.0
Demonstrate these symbols/APIs:
  - detectNewline
  - detectNewlineGraceful

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:1164809ff0597a2a7f5f8d7e8f1f92911a83ec8912ed7ae97851bd25752239b8","contract":["detectNewline returns \\r\\n when CRLF is the dominant newline character","detectNewline returns \\n when LF is dominant or when LF and CRLF counts are equal","detectNewline returns undefined when the string contains no newlines","detectNewline throws TypeError when called with a non-string argument","detectNewlineGraceful returns dominant newline character or defaults to \\n when no newline is present","detectNewlineGraceful returns \\n gracefully for non-string inputs without throwing"],"goal":"detect dominant newline character in string","kind":"HOW","packages":["pkg:npm/detect-newline@3.1.0"],"schemaVersion":1,"symbols":["detectNewline","detectNewlineGraceful"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/detect-newline@3.1.0"],"schemaVersion":1,"subject":"pkg:npm/detect-newline@3.1.0","symbols":["detectNewline","detectNewlineGraceful"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-detect-newline",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-detect-newline",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "detect-newline": "3.1.0"
      }
    },
    "node_modules/detect-newline": {
      "version": "3.1.0",
      "resolved": "https://registry.npmjs.org/detect-newline/-/detect-newline-3.1.0.tgz",
      "integrity": "sha512-TLz+x/vEXm/Y7P7wn1EJFNLxYpUD4TgMosxY6fAVJUnJMbupHBOncxyWUG9OpTaH9EBD7uFI5LfEgmMOc54DsA==",
      "license": "MIT"
    }
  }
}
package.json
{
  "name": "sample-detect-newline",
  "version": "1.0.0",
  "type": "module",
  "private": true,
  "license": "MIT-0",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "detect-newline": "3.1.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "detect dominant newline character in string",
  "kind": "HOW",
  "packages": [
    "pkg:npm/detect-newline@3.1.0"
  ],
  "symbols": [
    "detectNewline",
    "detectNewlineGraceful"
  ]
}
src/index.mjs
import detectNewline from 'detect-newline';

const detectNewlineGraceful = detectNewline.graceful;

/**
 * Detect the dominant newline character of a string.
 * @param {string} string - The input string to inspect.
 * @returns {'\r\n' | '\n' | undefined} The dominant newline character or undefined.
 */
export function detect(string) {
  return detectNewline(string);
}

/**
 * Detect the dominant newline character of a string, defaulting to '\n' on missing or invalid input.
 * @param {unknown} [string] - The input to inspect.
 * @returns {'\r\n' | '\n'} The dominant newline character or '\n'.
 */
export function detectGraceful(string) {
  return detectNewlineGraceful(string);
}

export { detectNewline, detectNewlineGraceful };
export default detectNewline;
test/contract.mjs
import assert from 'node:assert/strict';
import { detectNewline, detectNewlineGraceful, detect, detectGraceful } from '../src/index.mjs';

// 1. detectNewline returns \r\n when CRLF is the dominant newline character
{
  assert.equal(detectNewline('foo\r\nbar\r\nbaz\n'), '\r\n');
  assert.equal(detectNewline('\r\n\r\n'), '\r\n');
  assert.equal(detectNewline('line1\r\nline2\r\nline3\nline4\r\n'), '\r\n');
  assert.equal(detect('header\r\nbody\r\nfooter\n'), '\r\n');
}

// 2. detectNewline returns \n when LF is dominant or when LF and CRLF counts are equal
{
  assert.equal(detectNewline('foo\nbar\nbaz\r\n'), '\n');
  assert.equal(detectNewline('foo\nbar\n'), '\n');
  assert.equal(detectNewline('foo\r\nbar\n'), '\n');
  assert.equal(detectNewline('a\r\nb\nc\r\nd\n'), '\n');
  assert.equal(detect('first\nsecond\r\nthird\n'), '\n');
}

// 3. detectNewline returns undefined when the string contains no newlines
{
  assert.equal(detectNewline('foo bar baz'), undefined);
  assert.equal(detectNewline(''), undefined);
  assert.equal(detectNewline('   '), undefined);
  assert.equal(detect('single-line text without breaks'), undefined);
}

// 4. detectNewline throws TypeError when called with a non-string argument
{
  assert.throws(() => detectNewline(null), {
    name: 'TypeError',
    message: 'Expected a string'
  });
  assert.throws(() => detectNewline(undefined), {
    name: 'TypeError',
    message: 'Expected a string'
  });
  assert.throws(() => detectNewline(123), {
    name: 'TypeError',
    message: 'Expected a string'
  });
  assert.throws(() => detectNewline({}), {
    name: 'TypeError',
    message: 'Expected a string'
  });
  assert.throws(() => detectNewline(['\n']), {
    name: 'TypeError',
    message: 'Expected a string'
  });
  assert.throws(() => detect(true), {
    name: 'TypeError',
    message: 'Expected a string'
  });
}

// 5. detectNewlineGraceful returns dominant newline character or defaults to \n when no newline is present
{
  assert.equal(detectNewlineGraceful('foo\r\nbar\r\nbaz\n'), '\r\n');
  assert.equal(detectNewlineGraceful('foo\nbar\nbaz\r\n'), '\n');
  assert.equal(detectNewlineGraceful('foo bar baz'), '\n');
  assert.equal(detectNewlineGraceful(''), '\n');
  assert.equal(detectGraceful('single line text'), '\n');
  assert.equal(detectGraceful('multi\r\nline\r\ntext'), '\r\n');
}

// 6. detectNewlineGraceful returns \n gracefully for non-string inputs without throwing
{
  assert.equal(detectNewlineGraceful(null), '\n');
  assert.equal(detectNewlineGraceful(undefined), '\n');
  assert.equal(detectNewlineGraceful(), '\n');
  assert.equal(detectNewlineGraceful(123), '\n');
  assert.equal(detectNewlineGraceful({}), '\n');
  assert.equal(detectNewlineGraceful(['\r\n']), '\n');
  assert.equal(detectNewlineGraceful(false), '\n');
  assert.equal(detectGraceful(null), '\n');
}

console.log('All contract tests passed.');

오리진 시더

익명