Total.js as helper without web server
Only a few developers know that Total.js framework can be used without web server. Yes, Total.js framework is really great assistant for developer or administrator.
Installation
Installation is very easy and you can do it in less than a minute.
- if you don't have Node.js platform, install it
- then install Total.js as a global module, open terminal and run
$ npm install -g total.js
Functionality
If you have installed Total.js framework then you have a small canon available at Your disposal:
- methods for creating requests
- functions for image manipulation
- JavaScript, CSS and HTML minifications
String
operations
Number
operations
Date
operations
Array
operations
- tools for parsing data
- functions for sending email and checking SMTP servers
- async tools
Make sure to always add require('total.js')
.
Parsing XML
A size of XML is not limited, everything is streamed.
require('total.js');
U.download('http://www.w3schools.com/xml/cd_catalog.xml', ['get'], function(err, response) {
response.on('data', U.streamer('<CD>', '</CD>', function(element, index) {
// element contains parsed XML ELEMENT STRING
var item = element.parseXML();
console.log(item['CD.ARTIST'], item['CD.PRICE'].parseFloat());
}));
});
Parsing CSV
A size of CSV is not limited, everything is streamed.
require('total.js');
U.download('http://www.sample-videos.com/csv/Sample-Spreadsheet-1000-rows.csv', ['get'], function(err, response) {
response.on('data', U.streamer('\n', function(line, index) {
var arr = line.split(',');
console.log(arr[1]);
}));
});
Making requests with RESTBuilder
require('total.js')
RESTBuilder.make(function(builder) {
// builder === instance of RESTBuilder
builder.url('https://api.yourdomain.com/users/create/');
builder.header('X-Token', '34873947A9D79A7937493749');
builder.post({ firstname: 'Peter', lastname: 'Sirka', web: 'https://www.totaljs.com' });
builder.exec(function(err, response) {
console.log(response.success);
});
});
Sending emails
require('total.js');
var mail = Mail.create('Subject', 'HTML body');
mail.to('petersirka@gmail.com');
mail.send('SMTP HOST', { user: 'XXX', password: 'XXX', timeout: 2000 });
Do you want to remove diacritics from a file?
const Fs = require('fs');
require('total.js');
Fs.readFile('somefile.txt', function(err, data) {
Fs.writeFile('somefile.txt', data.toString('utf8').removeDiacritics(), NOOP);
});
Listing directories and files
require('total.js');
U.ls('/some/path/to/directory/', function(directories, files) {
// directories = contains all directories
// files = contains all files
console.log(directories, files);
});
// If you need more details use U.ls2() which returns lists of objects (created date, modified date, length, etc.)
Resizing pictures
You need to install GraphicsMagick or ImageMagick before you can use resizing.
require('total.js');
// Image.load('picture.jpg', true === imageMagick or false === grapchismagick);
Image.load('picture.jpg', false).make(function(image) {
image.resize(200, 200);
image.quality(90);
image.minify();
image.save('resized.jpg');
});
Do you need a small NoSQL embedded database?
require('total.js')
NOSQL('users').find().make(function(filter) {
filter.where('age', '>', 20);
filter.where('confirmed', true);
filter.callback(function(err, response) {
console.log(err, response);
});
});
NOSQL('users').insert({ age: 32, firstname: 'Peter', lastname: 'Sirka', created: new Date(), confirmed: true });
Minifying JS, CSS and HTML
require('total.js');
// minify JavaScript
console.log(U.minifyScript('var a = 1 + 1;'));
// minify CSS
console.log(U.minifyStyle('body { margin: 5px; font-family: Arial; }'));
// minify HTML
// when HTML contains inline JS or CSS then the content is minified too
console.log(U.minifyHTML('<div>A</div> <div>B</div>'));
MD5, SHA1, SHA256, SHA512
require('total.js');
console.log('123456'.md5());
console.log('123456'.sha1());
console.log('123456'.sha256());
console.log('123456'.sha512());
Assign values according to path
require('total.js');
// long
var obj = {};
obj.address = {};
obj.address.city = {};
obj.address.city.name = 'Banska Bystrica';
// much shorter
var obj = {};
U.set(obj, 'address.city.name', 'Banska Bystrica');
console.log(obj);
console.log(U.get(obj, 'address.city.name'));
Built-in validation
require('total.js');
var email = 'petersirka@gmail.com';
var email_invalid = 'petersirka';
console.log(email.isEmail(), email_invalid.isEmail());
var phone = '+421903163302';
var phone_invalid = '349834';
console.log(phone.isPhone(), phone_invalid.isPhone());
var url = 'https://www.google.com';
var url_invalid = 'aldskjaljdadsôakjd';
console.log(url.isURL(), url_invalid.isURL());
var zip = '97401';
var zip_invalid = '34983983';
console.log(zip.isZIP(), zip_invalid.isZIP());
Simple string template engine
You can also use Total.js View Engine instead of the template engine below.
require('total.js');
var string = 'My name is {{ name }}. Created: {{ created | dd.MM.yyyy }} and credits: {{ credits | ## ### }} credits.';
console.log(string.params({ name: 'Peter', created: new Date(), credits: 1000 }));
// or simple format string.format() in C# style
var string_format = 'My name is {0} and current date time is: {1}.';
console.log(string_format.format('Peter', new Date().format('dd.MM.yyyy HH:mm'));
Total.js terminal app
If you have Total.js installed as a global module, then you can use Total.js terminal app.
Built-in web server with a directory listing
The command below starts a web server on the port 8000
and a public
directory will be current directory, so all files will be available trought the http
protocol. The web server tries to find file index.html
and if the file doesn't exist then directory listing is performed.
$ cd some-directory
$ totaljs 8000
Minify JS, CSS or HTML
$ totaljs --compress some-file.js
Documentation
Like the post? Read more features in documentation:
Support us on Total.js GitHub and social networks.