string-compiler

0.0.3 • Public • Published

String-Compiler v0.0.3 beta

This script compiles javascript code from a string and using a pattern string

Install:

$ npm install string-compiler

Import packages:

const { StringParser } = require('string-compiler')
 
StringParser.rules(/* text */, /* data */)

Testing commands:

$ npm test

Testing: ./test/test.*.js

JSON parse and use string-compiler

const fs = require('fs')
const { StringParser } = require('string-compiler')
 
let message = JSON.parse(fs.readFileSync('./test/messages/text.json', 'utf8'))
 
// Template strings are changed to transmitted data
console.log(StringParser.rules(message['test1'], { data: 'Hello World' }))
// Output: "Testing lib: Hello World"
 
// Or if there is no template line in the text and transfer data, then this data will be ignored
console.log(StringParser.rules(message['test2'], { data: 'Hello World' }))
// Output: "Testing lib!"
 
// Plain text output
console.log(StringParser.rules(message['test3']))
// Output: "Testing lib!"
 
// -- use more keys
 
console.log(StringParser.rules(message['go']['to']['name1'], { name: 'JavaScript' }))
// Output: "Hello JavaScript"
console.log(StringParser.rules(message['go']['to']['name2'], { name: 'JavaScript' }))
// Output: "Hello!"
console.log(StringParser.rules(message['go']['to']['name3']))
// Output: "Hello!"

Use string and use string-compiler

const { StringParser } = require('string-compiler')
 
// Template strings are changed to transmitted data
console.log(StringParser.rules('New message to the world: ${name}', { name: 'Hello World!' }))
// Output: "New message to the world: Hello World!"
 
// Or if there is no template line in the text and transfer data, then this data will be ignored
console.log(StringParser.rules('I am a programmer and I am writing?', { name: 'Hello World' }))
// Output: "I am a programmer and I am writing?"
 
// Plain text output
console.log(StringParser.rules('It turns out that { I } write hello world? $'))
// Output: "It turns out that { I } write hello world? $"

Package Sidebar

Install

npm i string-compiler

Weekly Downloads

2

Version

0.0.3

License

ISC

Unpacked Size

6.28 kB

Total Files

9

Last publish

Collaborators

  • fukachanyt