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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@yaufai/mytsutils

v0.0.8

Published

## getAllMatches

Downloads

11

Readme

正規表現

getAllMatches

正規表現がマッチするすべての結果を返します。通常の正規表現で利用されるg修飾子とは異なり、グループなどの情報を失いません。

import { getAllMatches } from "@yaufai/mytsutils"

let results = getAllMatches(/(?<language>[a-z]{2})-(?<country>[a-z]{2})/, "ja-jp,en-us")

console.log((actual[0].groups as {[key: string]: string})["language"]) 
// -> ja
console.log((actual[1].groups as {[key: string]: string})["country"]) 
// -> us

splitByRegexp

正規表現と文章を与えると、文章を正規表現で分割した文字列のリストを返します。

keepSeparatorを与えると分割に利用した、正規表現にマッチする箇所も保存されます。

import { splitByRegexp } from "@yaufai/mytsutils"

console.log(splitByRegexp(/[1-9]+/, "aa1aa332aa")) 
// -> [ "aa", "aa", "aa" ]
import { splitByRegexp } from "@yaufai/mytsutils"

console.log(splitByRegexp(/[1-9]+/, "aa1aa332aa", true)) 
// -> [ "aa", "1", "aa", "332", "aa" ]

NotImeplementedException

中身はとりあえず実装を後回しにして、先に関数やメソッドの型だけ定義したい場合に使用する独自例外です。

import { NotImplementedException } from "@yaufai/mytsutils"

function someFunction(x: number): number {
    throw new NotImplementedException()
}

tryToCompute

受け取った関数を例外処理でラップした新しい関数を返します。

新しい関数は、まず与えられた関数を実行し、例外が発生しなければその計算結果を返します。例外が発生した場合にはデフォルト値を返します。

また、計算結果を受け取ってブール値を返すavoid関数を与えることで、例外発生時以外にも計算結果がtrueと判定される場合にデフォルト値を返すようにすることもできます。

import { tryToCompute } from "@yaufai/mytsutils"

const detactYear: (x: string) => number = x => {
    const result = x.match(new RegExp(/\d{4}/))
    if (result === null) {
        throw new Error("No year found!")
    } else {
        return Number(Array.from(result)[0])
    }   
}
const defaultYear = 2021


tryToCompute(detactYear, defaultYear)("1999")
// 1999 (問題なく正規表現で年号を発見できる)
tryToCompute(detactYear, defaultYear)("19xx")
// 2021 (年号が発見できず例外が発生するのでデフォルト値が採用される)
tryToCompute(detactYear, defaultYear, (yr => yr <= 2021))("1999")
// 2021 (年号が発見できたが値がavoid関数でtrueと計算された)

never

nevernever => T型の関数です。

switch文でdefaultに分岐することがないことを保証したいときに使用します。もし条件分岐が足りていなかったりそれまでにreturnを忘れるなどで値がnever型になることを保証できない場合はコンパイルエラーを出して開発者に通知します。

import { never } from "@yaufai/mytsutils"

type SomeEnumType = "a" | "b"

function someFunc(val: SomeEnumType): string {
    switch (val) {
        case "a":
            // do whatever you want
            return "a"
        case "b":
            // do whatever you want
            return "b"
        default:
            return never<string>(val)
    }
}

atob/btoa

Base64のエンコードとデコードを行います。

import { atob, btoa } from "@yaufai/mytsutils"

console.log(atob("こんにちは、世界!"))
// -> 44GT44KT44Gr44Gh44Gv44CB5LiW55WM77yB
console.log(btoa("44GT44KT44Gr44Gh44Gv44CB5LiW55WM77yB"))
// -> こんにちは、世界!

ArrayOperations

cartesian

ArrayOperations.cartesianは二つの配列を受け取り、そのデカルト積を返します。

import { ArrayOperations } from "@yaufai/mytsutils"

console.log(
    ArrayOperations.cartesian<string, number>([ "a", "b" ], [ 1, 2, 3 ])
)
// -> 
// [
//     [ "a", 1 ],
//     [ "a", 2 ],
//     [ "a", 3 ],
//     [ "b", 1 ],
//     [ "b", 2 ],
//     [ "b", 3 ],
// ]

intersperse

ArrayOperations.cartesianは次の二つの引数をとります。

  • 割り込ませる値
  • 元の配列

そして、元の配列の隣り合った要素間すべてに「割り込ませる値」を挿入します。

import { ArrayOperations } from "@yaufai/mytsutils"

console.log(
    ArrayOperations.intersperse<number>(0, [1, 2, 3])
)
// [ 1, 0, 2, 0, 3 ]

Markdownパーサ

ParserUtils.printAST

ASTは何も変更せず代わりに現時点でのASTを出力するRemarkプラグインです。主としてデバッグ用に使用します。

import { ParserUtils } from "@yaufai/mytsutils"

const processor = remark()
    .use(ParserUtils.printAST)
    .freeze()
processor.processSync(text)

ParserUtils.splitTextLiteralByRegex

Textリテラルノードを正規表現を使って分解します。

これ自体はRemarkプラグインではありませんが、Remarkプラグインを作成するときに有用です。

import { ParserUtils } from "@yaufai/mytsutils"

const pattern  = /\s+->\s+/
const textLiteral = {
    type: 'text',
    value: 'aaa -> b'
}
console.log(ParserUtils.splitTextLiteralByRegex(
    textLiteral,
    pattern
))
// [
//     {
//         type: 'text',
//         value: 'aaa'
//     },
//     {
//         type: 'text',
//         value: ' -> '
//     },
//     {
//         type: 'text',
//         value: 'b'
//     }
// ]

processorを与えると、分離に使った正規表現にマッチする箇所をさらに加工することができます。

import { ParserUtils } from "@yaufai/mytsutils"

const pattern  = /\s+->\s+/
const textLiteral = {
    type: 'text',
    value: 'aaa -> b'
}
console.log(ParserUtils.splitTextLiteralByRegex(
    textLiteral,
    pattern,
    (_t) => ({ type: 'text', value: "→" })
))
// [
//     {
//         type: 'text',
//         value: 'aaa'
//     },
//     {
//         type: 'text',
//         value: '→'
//     },
//     {
//         type: 'text',
//         value: 'b'
//     }
// ]

remarkBracketVariable

[カテゴリ:変数名]![カテゴリ:変数名]の形をした領域を別の文字列に変換するRemarkプラグインです。カテゴリは省略することもできますが、取得された場合には変換を出し分けるのに活用できます。

デフォルトでは、変数名をそのまま出力します。

compileInlinecompileBlockを与えることで変換先を変更できます。

  • compileInline<T>: (value: string, category: string) => T|string
  • compileBlock<T> : (value: string, category: string) => T|string

返り値はASTのノードか文字列である必要があり、文字列の場合には自動的にテキストリテラルに変換されます。