jStrip
jStrip is written to run with Node.js and let's you easily grab data from the web - from json
, html
, array
, number
or from text and apply multiple filters to change the data to your liking, before it is returned to you asynchronously. jStrip works great with json
formats, making json API work a breeze. Also apply jQuery to html
pages, locally or from the web.
Use chainable methods and event handlers to enhance jStrip, with new features being added regularly.
To migrate any v1.x
code see the Migration section below.
Chuck Norris Random Joke - json api
const jStrip = ; const chuckNJoke = ; //display random chuck norris jokechuckNJoke; chuckNJoke ; //jpretty layouts json in easy to read format
Check out more Examples below.
Table of Contents
- Installation
- Chainable Methods
- Async Event Handler
- Migration from v.1.x
- Examples
- Testing
- Versioning
- Getting Involved
- Authors
- Licence
Installation
Install jstrip with npm:
$ npm i -S jstrip
Include jStrip and create an instance.
const jStrip = ; //top of file...let jStrip1 = ;
Chainable Methods
jStrip allows you to append as many filters as you like by simply chaining methods together with a dot delimiter. For example:
jStrip4 ; //displays current time in new zealand
First method - .getData( url | text | json | array | number )
The only requirement is to first grab the data. Start by using the .getData()
method.
getData()
accepts 3 string types: a "url", "text" or json. If a url returns json
, the type will become json.
jStrip1 // url//orjStrip1 // string//orjStrip1 // jsonjStrip1 // json // (as a string)jStrip // number//orjStrip1 // json
If you use a url the default timeout for the http/s request is 10000 milli-seconds. You can change this per instance by changing the timeout
property.
jStrip1timeout = 15000;console; // 15000 - 15 seconds
.show()
show()
displays the contents to the console. Great for seeing the results, see .marker() and .on() below to add an event handler.
jStrip1 //hello world
.pretty()
pretty()
will format the data it is given. This is great for tidying html, xml or standard text.
jStrip1 ; // hello world (removes extra spaces)
.jpretty()
Make json
data more readable in a key=value layout. jpretty()
outputs the json
format into an easy to read layout.
const jStrip18 = ; jStrip18; // .bpi.USD.rate is easy seen from jpretty output jStrip18 ;
jpretty().show()
console output of json:
{}.bpi.USD.code = USD{}.bpi.USD.rate = 8,262.8113{}.bpi.USD.description = United States Dollar{}.bpi.USD.rate_float = 8262.8113
jpretty()
outputs the json
into a dot delimited layout of key = value, making it easy to identify its fields and values. Note that jpretty()
changes the json
into a different format, so any property access using the object must be performed beforehand, as in the example above.
.selector(jquery)
selector()
lets you grabs html from the data using jQuery functionality. Check out the many available jQuery selectors you can use.
jStrip1 ; //2nd li tag inside div with id 'top'
.replace(pattern, text)
Use a regular expression or string pattern to search the jStrip content, followed by the replacement string.
jStrip5 // ki ora world
.removehtml()
html elements are stripped out.
jStrip5 // hello world
.uppercase()
convert string contents to upper case.
jStrip5 // HELLO WORLD
.lowercase()
convert string contents to lower case.
jStrip5 // hello world
.sort()
sorts an array
, string
or url
contents into alphabetical / numeric order.
jStrip5 //[ 1, 2, 3, 4, 5 ]
The string
/ url
has a maximum string length of 100 characters.
.reverse()
convert an array
, string
or url
into reverse order.
jStrip5 //[ 5, 4, 3, 2, 1 ]
The string
/ url
has a maximum string length of 100 characters.
.add(number)
performs an mathematical addition. The getData value needs to be a number.
jStrip5 // 2000
.minus(number)
performs an mathematical subtraction. The getData value needs to be a number.
jStrip5 // 1000
Async Event Handlers .marker() and .on()
Grabbing data from the web is not instant, so jStrip caches the filters and provides an event handler to tell you when it has all it's data ready, and send it asynchronously without blocking.
Set a marker (or many) in your jStrip call, simply by using the marker() method and giving it a unique name.
.marker(eventname)
jStrip1 // 1st marker ; // 2nd marker
Display the markers asynchronously with the on() event handler. Two parameters are needed: the named marker() to trigger on followed with a function, jStrip returns an object to the function, with 4 properties:
data
- containing the content, as a stringtype
- datatype (string | url | json )url
- url address used if type = 'url', otherwise equalsundefined
.
.on( marker name, callback fn(returned data) )
jStrip1; jStrip1;
- The on() event handler(s) should be placed before the marker() is set.
Migrating from Version 1 to Version 2
To keep your v 1.x
code working with v 2.x
, make these two simple updates to your existing code:
- Create a seperate instance for each jStrip call, with the new operator.
let jStripInstance1 = ; //new version 2 way
- Replace your jStrip call with _jStrip():
//older version 1 way
to
jStripInstance1 //new version 2 way
That's all!
You can still use Version 1
features together with Version 2
.
_jStrip( "url", "jquery" )
_jStrip() returns data via a Promise or as Async/Await.
Using Promises
// Using PromisejStripInstance1 ;
Using Async/Await
// Using Async/Awaitconst fn = async { try const result = await jStripInstance1; await console; catch err console; };
Examples
Chuck Norris Random Joke - json api
const jStrip = ; const chuckNJoke = ; //display random chuck norris jokechuckNJoke; chuckNJoke ; //jpretty layouts json in easy to read format
Live Bitcoin Rates - json api
const bitcoinRates = ; bitcoinRates; bitcoinRates ;
Live Time in New Zealand - html
const nzTime = ; nzTime nzTime ;
textchange - text
const text = ; text text ;
add/subtract - number
const num = ; num num ;
Tests
Automated CI test builds are run with each update.
Versioning
We use SemVer for versioning.
Get Involved
Issues
If you have an issue, or a bug let us know and we will do our best.
Create an issue here.
Contributing
If you have any questions, comments, suggestions or ideas, feel free to drop me a line. We'd love to hear your suggestions and ideas!
Authors
- Frank C - frankc60