1.0.4 • Published 3 years ago

@typicalninja21/placeholder v1.0.4

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

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 or I 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
1.0.4

3 years ago

1.0.2

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago