1.0.10 • Published 4 years ago

printin v1.0.10

Weekly downloads
-
License
ISC
Repository
github
Last release
4 years ago

PrintIn

This is a tool used to print data in a proper format according to your needs. This use template strings to make printing flexible. Use {} in your string and pass the variable as parameters or pass them later by calling the returned function.

Comments


You can use comments in {} by writing the text between ~ for better readability.

print("{~message~} {~name~}", "Hello", "Node");
>> Hello Node

print("{~resource~} {}", "Memory", "Stack Overflow");
>> Memory Stack Overflow

Escape Character


To esacpe the brackets and prevent yourself from errors, use {{ and }} and the code will treat the brackets like normal characters.

print("{~message~} {~name~} {{🙃}}", "Hello", "Node");
>> Hello Node {🙃}

Referencing


  • Empty

You can simpy use {} where you want to substitute data in the string. The data will be picked and placed from left to right. Brackets with comments ( eg. {~message~} ) will be also treated as empty brackets.

print("{} {} {} {}", "A")("B", "C", "D")
>> A B C D
  • Indexes

Sometimes the order of passed elements does not match their order in the string. Use Indexes starting from 0 and the function will replace the passed Strings or Data from left to right as if they are in array.

print("{1} {3} {2} {0}", "A", "B", "C", "D")
>> B D C A
  • Keys

You can also pass valid JS keys between the {} and then pass the JS Object to with the same keys and their values. Values will be overridden from left to right.

print("{message} {name}", { message: "Hello", name: "Nathan" })
>> Hello Nathan
  • Arrays

Pass the array rather together rather than passing the parameters individually. The values are extracted from the array and sorted out in 2 portions, ie. Strings and Numbers; and Objects. The Strings are then added from left to right. The Objects are evaluated in the same way.

print("{1} {3} {2} {0}", ["A", ["B", "C"]], ["D"])
>> B D C A

Try combining it with chalk.js to see the different possiblities.

Current State

Current state of the string in returned function:

const message = print("{~message~} {~name~}", "Hello");
console.log(message.string);
>> Hello {~name~}

Current state of unused brackets in returned function:

const message = print("{~message~} {~name~}", "Hello");
console.log(message.unused);
>> [ { unused: '{~name~}', index: 6 } ]

Examples


CODE

Code Example

OUTPUT

Console Output

Note

The functions will be returned until all the brackets are occupied.

In the case of strings like

print("{5}")

You will need to pass 6 arguments where the first 5 arguments will be dumped and the 6th one is placed in the string.

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.10

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago