Total.js as helper without web server

Thu Nov 03 2016 10:47:43 GMT+0100 (Central European Standard Time), Peter Širka

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.

$ totaljs --help

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.


Tags

Follow us

Latest blogs
Flow v6 is here!
Thu Mar 07 2019 11:53:54 GMT+0100 (Central European Standard Time)
A critical security fix
Wed Feb 13 2019 22:15:39 GMT+0100 (Central European Standard Time)
New release: Total.js v3.2
Wed Feb 13 2019 22:14:39 GMT+0100 (Central European Standard Time)
Total.js Wiki v2
Fri Jan 04 2019 22:15:01 GMT+0100 (Central European Standard Time)
Total Year 2018
Thu Jan 03 2019 21:14:00 GMT+0100 (Central European Standard Time)

Latest comments
Nice tip
Mauro Junior
Thu Sep 20 2018 21:41:02 GMT+0200 (Central European Summer Time)
Not only for Total.js. You can communicate with different websocket servers.
Peter Širka
Mon Apr 23 2018 20:08:20 GMT+0200 (Central European Summer Time)
Marko: you need to create a buffer with this codepage and write byte-to-byte string. I recommend ...
Peter Širka
Mon Apr 23 2018 20:06:21 GMT+0200 (Central European Summer Time)
Is WEBSOCKETCLIENT only for internal ws connections between totaljs apps?
Stelios Stephanua
Fri Mar 16 2018 06:04:22 GMT+0100 (Central European Standard Time)
Total.js is amazing! ;)
Leonardo Hessel
Tue Dec 19 2017 19:51:15 GMT+0100 (Central European Standard Time)

Pixabay


Read more

Flow v6 is here!

News: We have released a new version of Flow. This version brings great new features and UI improvements.

Thu Mar 07 2019 11:53:54 GMT+0100 (Central European Standard Time)
A critical security fix

News: We were notified about the critical security bug in Total.js framework. Read a prevention.

Wed Feb 13 2019 22:15:39 GMT+0100 (Central European Standard Time)
New release: Total.js v3.2

News: This new release brings a critical security fix and small new improvements. Update Total.js now.

Wed Feb 13 2019 22:14:39 GMT+0100 (Central European Standard Time)
Total.js Wiki v2

Products: I have released a new version of Total.js Wiki. New version brings new improvements.

Fri Jan 04 2019 22:15:01 GMT+0100 (Central European Standard Time)
Total Year 2018

Business: Last year was perfect for Total.js platform. Total.js platform grows up and it has great results.

Thu Jan 03 2019 21:14:00 GMT+0100 (Central European Standard Time)
Total.js Code Editor v1

Products: Try our real-time collaboration tool for Total Developers. Code Editor offers great features for development.

Fri Dec 07 2018 22:55:13 GMT+0100 (Central European Standard Time)