npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

text-kind

v1.0.0

Published

Detect and classify text kinds: western, CJK, data/code, mixed, or unknown

Readme

text-kind

A TypeScript/JavaScript library to detect and classify text kinds: western, CJK, data/code, mixed, or unknown.

Installation

npm install text-kind

Usage

import { detectTextKind } from 'text-kind';

// Detect English text
const result1 = detectTextKind('Hello world, this is some English text.');
console.log(result1.kind); // 'western'
console.log(result1.confidence); // 0.834

// Detect CJK text
const result2 = detectTextKind('これは日本語のテキストです。');
console.log(result2.kind); // 'cjk'

// Detect JSON
const result3 = detectTextKind('{"name": "John", "age": 30}');
console.log(result3.kind); // 'data_code'
console.log(result3.details.jsonLikely); // true

// Detect CSV
const result4 = detectTextKind(`name,age,city
John,25,NYC
Jane,30,LA`);
console.log(result4.kind); // 'data_code'
console.log(result4.details.csvLikely); // true

API

detectTextKind(text: string, sampleSize?: number): DetectionResult

Analyzes the provided text and returns a classification result.

Parameters:

  • text - The text to analyze
  • sampleSize - Optional. Maximum number of characters to analyze (default: 20000)

Returns: A DetectionResult object containing:

interface DetectionResult {
  kind: 'western' | 'cjk' | 'data_code' | 'mixed' | 'unknown';
  confidence: number; // 0 to 1
  scores: { 
    western: number; 
    cjk: number; 
    data_code: number; 
  };
  reasons: string[]; // Human-readable detection signals
  details: DetectionDetails; // Detailed character counts and flags
}

Text Kinds

  • western: ASCII/Latin-based languages (English, Spanish, French, etc.)
  • cjk: Chinese, Japanese, Korean text
  • data_code: JSON, CSV/TSV, SQL, or source code
  • mixed: Text that has significant presence of multiple kinds
  • unknown: Text that doesn't clearly fit into other categories

Features

  • Unicode-aware: Uses Unicode property escapes when available, falls back to character ranges
  • Multiple format detection: JSON, CSV/TSV, SQL, and general source code patterns
  • Confidence scoring: Provides confidence levels and detailed reasoning
  • Efficient: Processes large texts by sampling (configurable sample size)
  • Detailed analysis: Returns character counts, script distributions, and detection flags

Examples

Mixed Content

const mixed = detectTextKind('Hello 世界! {"status": "ok"}');
console.log(mixed.kind); // 'mixed'
console.log(mixed.reasons); // ['CJK characters: 2', 'ASCII letters: 5', ...]

Code Detection

const code = detectTextKind(`
function hello() {
  console.log("Hello world");
  return true;
}
`);
console.log(code.kind); // 'data_code'
console.log(code.details.codeTokenHits); // 4

License

MIT