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

string-templating

v3.0.0

Published

template strings and output them to a file (use-case: sql queries, ML datasets)

Downloads

8

Readme

string templating

template strings and output them to a file (use-case: sql queries, ML datasets)

npm i --save string-templating

usage

import strTempl from 'string-templating'

const output = strTempl({
	amount: 5,
	template: '${iterator.num}+${iterator.num}=${returners.sum}',
	iterators: {
		num: function*() {
			yield* [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
		}
	},
	returners: {
		sum: iterVals => iterVals.num[0] + iterVals.num[1]
	},
	recycle: true
})

console.log(output) // [`1+2=3`, `3+4=7`, `5+6=11`, `7+8=15`, `9+10=19`]

config

interface IStringifiable {
	toString: () => string
}

type IteratorMap = {
	[key: string]: () => Iterator<IStringifiable>
}

type ReturnerMap = {
	[key: string]: (iteratorValues: unknown) => IStringifiable
}

interface ITemplatingOptions {
	amount: number
	template: string
	outFile?: fs.PathLike
	iterators: IteratorMap
	returners?: ReturnerMap
	recycle?: boolean
}
  • amount: amount of strings to be generated
  • template: the string to be templated. ${iterators.yourname} in the string will be replaced with values from your iterators; ${returners.yourname} in the string will be replaced with values from your returners
  • outFile: if you wish for the output to be saved to a file, specify a file path. If your file will have a json extention it will be saved as a json array
  • iterators: object with your iterators
  • returners: object with your returners. A returner will get all generated values from iterators as an object. Values will be stored as an array (if recycling is ON) or plain value (if recycling is OFF)
  • recycle: if true, new values will be generated for each replacement in the template (otherwise each template generation gets one value)

helper functions

General purpose helper functions typical for string templating

range

import { range } from 'string-templating'
export function range(to: number): Iterable<number>
export function range(from: number, to: number, step?: number): Iterable<number>
for (let i of range(10)) console.log(i)
/*
0
1
2
3
4
5
6
7
8
9
*/
for (let i of range(7, 11)) console.log(i)
/*
7
8
9
10
*/
for (let i of range(5, 13, 3)) console.log(i)
/*
5
8
11
*/

random

import { random } from 'string-templating'
export function random(to: number, decimal?: boolean): number
export function random(from: number, to: number, decimal?: boolean): number
export function random<T>(array: T[]): T
random(5) // integer in the range of [0, 5)
random(5, true) // decimal in the range of [0, 5)

random(-3, 4) // integer in the range of [-3, 4)
random(-3, 4, true) // decimal in the range of [-3, 4)

random([1, true, 'whoop']) // returns random element from the array

examples

cd examples -> npm i -> node <example_name>

operations

Generates train.txt and test.txt. The files contain calculations: {num1}{operand}{num2}={result}. A very simple example of a dataset generated for a basic machine learning problem.

typescipt

strongly typed returners

Returners get iterator values as an unknown. There is a helper type ExtractIteratorValues to help you strongly type the values:

import StrTempl, { ExtractIteratorValues, ITemplatingOptions } from 'string-templating'

const iterators = {
	num: function*() {
		yield* [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
	}
}

const config: ITemplatingOptions = {
	amount: 5,
	template: '${iterators.num}+${iterators.num}=${returners.sum}',
	iterators,
	returners: {
		sum: (iterVals: ExtractIteratorValues<typeof iterators, true>) =>
			iterVals.num[0] + iterVals.num[1]
	},
	recycle: true
}

const result = StrTempl(config)

Pass in your iterators and whether you're recycling them.