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

@kotaicodegmbh/k8s-resources

v0.1.2

Published

A TypeScript library for performing arithmetic and comparison operations on Kubernetes CPU and memory resources

Downloads

216

Readme

Kubernetes Resource Arithmetic Library

A TypeScript library for performing arithmetic and comparison operations on Kubernetes CPU and memory resources.

Features

  • Parse and validate Kubernetes CPU and memory resource strings
  • Perform arithmetic operations (addition, subtraction, multiplication)
  • Compare resource values
  • Format resources in human-readable Kubernetes units
  • Type-safe operations with TypeScript
  • Comprehensive test coverage

Installation

npm install @kotaicodegmbh/k8s-resources

Usage

CPU Resources

import { CPUResource, MemoryResource } from '@kotaicodegmbh/k8s-resources';

// Create CPU resources
const cpu1 = new CPUResource('100m');  // 100 millicores
const cpu2 = new CPUResource('1');     // 1 core
const cpu3 = CPUResource.fromMillicores(500);  // 500 millicores
const cpu4 = CPUResource.fromCores(2);         // 2 cores

// Arithmetic operations
const sum = cpu1.plus(cpu2);           // 1.1 cores
const diff = cpu2.minus(cpu1);         // 900m
const scaled = cpu1.times(3);          // 300m

// Comparison operations
const isLess = cpu1.isLessThan(cpu2);  // true
const isEqual = cpu1.equals(cpu3);      // false
const isGreater = cpu2.isGreaterThan(cpu1); // true

// String representation
console.log(cpu1.toString());  // "100m"
console.log(cpu2.toString());  // "1"

Memory Resources

import { MemoryResource } from '@kotaicodegmbh/k8s-resources';

// Create memory resources
const mem1 = new MemoryResource('128Mi');  // 128 mebibytes
const mem2 = new MemoryResource('1Gi');    // 1 gibibyte
const mem3 = MemoryResource.fromBytes(1024 * 1024);  // 1MiB
const mem4 = MemoryResource.fromGiB(2);              // 2GiB

// Arithmetic operations
const sum = mem1.plus(mem2);           // 1.125Gi
const diff = mem2.minus(mem1);         // 896Mi
const scaled = mem1.times(2);          // 256Mi

// Comparison operations
const isLess = mem1.isLessThan(mem2);  // true
const isEqual = mem1.equals(mem3);      // false
const isGreater = mem2.isGreaterThan(mem1); // true

// String representation
console.log(mem1.toString());  // "128Mi"
console.log(mem2.toString());  // "1Gi"

API Reference

The API documentation is available in the docs/api directory after running npm run docs. You can find:

CPUResource

Constructor

new CPUResource(resource: string)

Creates a new CPU resource from a string representation (e.g., "100m" or "1").

Static Factory Methods

CPUResource.zero(): CPUResource
CPUResource.fromMillicores(millicores: number): CPUResource
CPUResource.fromCores(cores: number): CPUResource

Instance Methods

plus(other: CPUResource): CPUResource
minus(other: CPUResource): CPUResource
times(factor: number): CPUResource
equals(other: CPUResource): boolean
isLessThan(other: CPUResource): boolean
isGreaterThan(other: CPUResource): boolean
toString(): string

MemoryResource

Constructor

new MemoryResource(resource: string)

Creates a new memory resource from a string representation (e.g., "128Mi" or "1Gi").

Static Factory Methods

MemoryResource.zero(): MemoryResource
MemoryResource.fromBytes(bytes: number): MemoryResource
MemoryResource.fromKiB(kib: number): MemoryResource
MemoryResource.fromMiB(mib: number): MemoryResource
MemoryResource.fromGiB(gib: number): MemoryResource

Instance Methods

plus(other: MemoryResource): MemoryResource
minus(other: MemoryResource): MemoryResource
times(factor: number): MemoryResource
equals(other: MemoryResource): boolean
isLessThan(other: MemoryResource): boolean
isGreaterThan(other: MemoryResource): boolean
toString(): string

Resource Units

CPU Units

  • m: millicore (1/1000 of a CPU core)
  • No suffix: CPU cores (e.g., "1" = 1000m)

Memory Units

  • B: bytes
  • Ki: kibibytes (1024 bytes)
  • Mi: mebibytes (1024 KiB)
  • Gi: gibibytes (1024 MiB)
  • Ti: tebibytes (1024 GiB)

Error Handling

The library throws errors in the following cases:

  • Invalid resource format
  • Negative values
  • Non-finite numbers
  • Fractional values (for CPU millicores and memory bytes)
  • Invalid units
  • Negative results from subtraction
  • Invalid multiplication factors

Development

# Install dependencies
npm install

# Run tests
npm test

# Build
npm run build

# Generate API documentation
npm run docs

The API documentation will be generated in the docs/api directory. The documentation includes:

  • Detailed class and method descriptions
  • Type information
  • Examples
  • Error documentation
  • Cross-references between related components

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

MIT