@typicalninja21/placeholder v1.0.4
placeholder
tbh this shouldn't be a package ;(
Simply replace everything marked as a placeholder on a String with values provided by you.
Enough explanations, you can get what this package does from the below examples
Support
you can also get support for that package below
tho why would you need support for this small project
examples
Simple
const parser = require('@typicalninja21/placeholder')
const string = 'Hey {user} whats up, i am {mood}. have you ever used {package}. its sooo {mood}';
(async () => {
console.log(await parser.format({
str: string,
delimiters: {
beg: '{',
end: '}',
args: ':',
argsSep: ','
},
matches: [
{ value:'user', replace: 'typicalninjaiscool'},
{ value: 'mood', replace: 'good'},
{ value: 'package', replace: '@typicalninja/placeholder'}
],
}))
})()
this will log:
Hey typicalninjaiscool whats up, i am good. have you ever used @typicalninja/placeholder. its sooo good
, we await the parse function as it returns a promise
Thats it? no
args (random item from the given args)
const parser = require('@typicalninja21/placeholder')
const string = 'I selected {random:head,tails}';
(async () => {
console.log(await parser.format({
str: string,
delimiters: {
beg: '{',
end: '}',
args: ':',
argsSep: ','
},
holders: [
{ value:'random', parseArgs: (Args, originalText) => {
return Args[~~(Math.random() * Args.length)]
}
},
],
}))
})()
This should log: Either :
I selected head
orI selected tails
Parsing a string
Want to just parse a string and get all the placeholders in it but don't want to format the string? then use the parse function exported by the module, you can see a example below
const parser = require('@typicalninja21/placeholder')
const string = 'so parsing {who} huh? remember it gives {return}, hope you {random:like, no like4 u}';
(async () => {
console.log(parser.parse({
str: string,
delimiters: {
beg: '{',
end: '}',
args: ':',
argsSep: ','
},
}))
})()
this will log the following array with objects
[
{ name: 'who', withArgs: 'who' },
{ name: 'return', withArgs: 'return' },
{
name: 'random',
withArgs: 'random:like, no like4 u',
args: [ 'like', ' no like4 u' ],
totalArgs: 2
}
]
Additonl notes
- Invalid (match not found in given matches) one will be ignored, want them to be replaced by something else? pass
invalid
options with others, value being a string of whatever you want it to replace invalid stuff with