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

xjs-common

v13.5.2

Published

library modules for typescript that bundled general-purpose implementations.

Readme

npm CI publish

Overview

Library modules for typescript that bundled general-purpose implementations.
This module is very simple, therefore it has no dependencies.

Install

npm i xjs-common

NOTE: features related nodejs was moved to xjs-node at v10.0.0.
if you has been used the features (e.g. HttpResolver, UFile), please use the new package instead of this.

Code example (only part)

Miscellaneous utilities.

import { delay, int2array, UHttp, retry, MaybeArray, Loggable, valueof } from "xjs-common";

(async () => {
    // await 3 seconds.
    await delay(3);

    // [ 0, 1, 2, 3, 4 ]
    console.log(int2array(5));

    // runs callback with customizable retry.
    retry(async () => { }, { count: 2 });

    // utility types
    let dateCtor: Ctor = Date; // constructor type like class type.
    let maybeArray: MaybeArray<number> = 0; // also number array is applicable.
    let logger: Loggable = console; // object implements log/warn/error is applicable.

    // true
    console.log(UHttp.isHttpSuccess(204));
    // https://aaa.com?p1=a&p2=1&p2=2
    console.log(UHttp.concatParamsWithEncoding("https://aaa.com", { p1: "a", p2: ["1", "2"] }));
    // checks and casts a value like enum valueof.
    enum EnumA { A = "a", B = "b" }
    const e: EnumA = valueof(EnumA, "a");
})();

Array utilities.

import { UArray } from "xjs-common";

(() => {
    // [ 1, 3, 2, 5, 4 ]
    const ary1 = UArray.distinct([1, 3, 2, 2, 1, 5, 4]);
    console.log(ary1);
    // [1, 1, 2, 2]
    console.log(UArray.duplicate([1, 3, 2, 2, 1, 5, 4]));

    // [ 5, 4 ]
    console.log(UArray.takeOut(ary1, v => v > 3));
    // [ 1, 3, 2 ]
    console.log(ary1);

    const ary2 = [1, 2, 3];
    // true
    console.log(UArray.eq(ary1, ary2));
    // false
    console.log(UArray.eq(ary1, ary2, { sort: false }));

    const ary3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    // [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ], [ 10 ] ]
    console.log(UArray.chop(ary3, 3));

    // randomization.
    console.log(UArray.shuffle(ary3));
    console.log(UArray.randomPick(ary3));
})();

String utilities.

import { UString } from "xjs-common";

(() => {
    // false
    console.log(UString.eq("", null));
    // true
    console.log(UString.eq("tanaka taro", "  tanaka taro  "));

    // e.g. cfNouG0P
    console.log(UString.generateRandomString(8));

    // 26
    console.log(UString.az2idx("AA"));
    // AA
    console.log(UString.idx2az(26));

    // e.g. 20250615053513
    console.log(UString.simpleTime());
    // e.g. 20250615
    console.log(UString.simpleTime({ date: getJSTDate(), unit: TimeUnit.Day }));
})();

Validate and crop object properties with annotation.

NOTE: this feature uses decorator, so requires "experimentalDecorators": true in tsconfig.
NOTE: some functionalities in this feature are based on "useDefineForClassFields": true in tsconfig.
this flag is true by default at the target higher than ES2022, here is for more.

import { Type, DType, UType, UObj } from "xjs-common";

interface If_B { aryB: number[], boolB: boolean, q: number }
class Cls_B implements If_B {
    @DType.array({ t: Type.number })
    aryB: number[];
    @DType.boolean
    boolB: boolean;
    q: number;
    constructor() { }
}
interface If_A { id: number, strA: string, objA: If_B, p: number }
class Cls_A implements If_A {
    @DType.required
    @DType.number
    id: number;
    @DType.string
    strA: string;
    @DType.object(Cls_B)
    objA: If_B;
    p: number;
    constructor(substance?: If_A) { Object.assign(this, substance); }
}
(() => {
    const valid_b1: If_B = { aryB: [1, 2, 3], boolB: true, q: 1 };
    const valid_a1: If_A = { id: 1, strA: "a", objA: valid_b1, p: 1 };

    // remove non decorated fields.
    const cropped = UObj.crop(valid_a1, Cls_A);
    console.log(!!cropped.id && !cropped.p); // true;
    console.log(!!cropped.objA.aryB && !cropped.objA.q); // true

    // passing class object instead of ctor is allowed.
    UObj.crop(new Cls_A(valid_a1));

    // validation. below are valid cases.
    console.log(UType.validate({ id: 0 }, Cls_A)); // []
    console.log(UType.validate(valid_a1, Cls_A)); // []

    // validation. below are invalid cases.
    const invalid1 = {};
    console.log(UType.validate(invalid1, Cls_A)); // [ 'id' ]

    const invalid2 = { id: 0, strA: [], objA: valid_b1 };
    console.log(UType.validate(invalid2, Cls_A)); // [ 'strA' ]

    const invalid3 = { id: "0", strA: "a", objA: valid_b1 };
    console.log(UType.validate(invalid3, Cls_A)); // [ 'id' ]

    const invalid_b1 = { aryB: [1, 2, 3], boolB: 1 };
    const invalid4 = { id: 0, strA: "a", objA: invalid_b1 };
    console.log(UType.validate(invalid4, Cls_A)); // [ 'objA.boolB' ]

    const invalid_b2 = { aryB: ["1"], boolB: true };
    const invalid5 = { id: 0, strA: "a", objA: invalid_b2 };
    console.log(UType.validate(invalid5, Cls_A)); // [ 'objA.aryB.0' ]
})();

Mark method as transaction.

NOTE: this feature uses decorator, so requires "experimentalDecorators": true in tsconfig.

import { transaction, delay } from "xjs-common";

class Cls {
    constructor() { }
    // default timeout sec is 30.
    @transaction()
    async exe1(): Promise<void> {
    }
    @transaction({ timeoutSec: 3 })
    async exe2(): Promise<void> {
        await delay(10);
    }
}
(async () => {
    const cls = new Cls();
    await Promise.all([cls.exe2(), cls.exe2()]);
})().catch(e => {
    // reach here after 3 sec from second call for Cls#exe2().
    // XjsErr [Error]: [XJS] An exclusive process to execute was already running by other request.
    console.log(e);
});

Error definition

XJS throws error with code property which has one of the following numbers. |code|thrown by| |:---|:---| |10|func/u| |20|func/u-string| |30|func/u-type (include func/decorator/d-type) | |100|func/decorator/transaction|

License

Apache-License