New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

stringf

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stringf

Dead simple string formatter

latest
Source
npmnpm
Version
1.0.4
Version published
Weekly downloads
5
-16.67%
Maintainers
1
Weekly downloads
 
Created
Source

Sringf License npm version

Dead simple cross-language string formatter

No dependencies, O(N*m)

Usage

go

import (
	"fmt"
	"github.com/thanhpk/stringf"
)

func main() {
	str := stringf.Format("hi {name}, here is your number {{2108}.", map[string]string{
		"name": "Kieu Thanh", // key should not contains spaces
	})
	fmt.Println(str)
}

// hi Kieu Thanh, here is your number {2108.

js

var stringf = require("stringf")

var str = stringf.Format("hi #name, here is your number {{2108.", {
	name: "Thanh",
})
console.log(str)

// hi Kieu Thanh, here is your number {2108.

Test

Go

go test

Js

npm test

Testcase

StringParameter mapOutput
hi {name}name: Thanhhi Thanh
hi {num}num: 2108hi 2108
{{abc}abc: bcd{abc

Pseudocode to implement in your own language

input:

a string, s
a map string to string, paramMap

output:

a string with param replaced

let ESCCHAR ← '{', ESCCHAREND ← '}', i ← 0, output ← ""
while i < length(s) do
	if s[i] = ESCCHAR then
		let j ← i + 1
		while j <= length(s) and s[j] = ESCCHAR do
			j ← j + 1
			if (j - i) mod 2 = 0 then
				output ← output + ESCCHAR
		if (j - i) mod 2 ≠ 0 then
			let param ← "" (parse parameter key)
			while j < length(s) and s[j] ≠ ESCCHAREND do
				param ← param + s[j]
				j ← j + 1
			if length(param) > 0 then
				output ← output + paramMap[param]
			if j = length(s) then
				return output
			else if s[j = ESCCHAREND then
				j ← j + 1
		i ← j
	output ← output + s[i]
	i ← i + 1
return output

Keywords

string

FAQs

Package last updated on 11 Sep 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts