Smscountjs

Get sms count (cyrillic, latin, french, german)

View the Project on GitHub likerRr/smscountjs

Demo

Limit:
Characters left:
SMS count:

Parts:

What is smscountjs?

This javascript plugin provides you some ways to obtain sms count and characters left via provided text string

Why i need it?

Sometimes may be useful to integrate into your project sms newsletter. So, once you've done it, it would be nice to have some hints (for those who will write sms text) such as total sms count and character left to next sms. At this point you need smscountjs

How can i use it?

Just include smscount.min.js to your html document, create SMS instance and call count method or use smsCount method of String object

Is it stable?

Current version is 1.0, it's stable and include these features:

Basic usage

Let's see, how it works by object oriented way:

var sms = SMS(),// also you can use "new" keyword
    text = 'Hello, world! This is a simple example of how to use "smscountjs" to calculate current total sms count and characters left to next sms. SMS in latin may contain up to 160 characters per SMS';

sms.count(text, function(totalCount, charsLeft, parts, limit) {
    console.log('total: ' + totalCount);
    console.log('left: ' + charsLeft);
    console.log('limit: ' + limit);
    console.log(parts);
    // get full message length
    console.log('length: ' + parts.join('').length);
    // get full message
    console.log('message: ' + parts.join(''));
});

Code above will output: Output_1

If you prefer to use smscount directly with String object, just make same:

var text = 'Hello, world! This is a simple example of how to use "smscountjs" to calculate current total sms count and characters left to next sms. SMS in latin may contain up to 160 characters per SMS';

text.smsCount(function(totalCount, charsLeft, parts, limit) {
    console.log('total: ' + totalCount);
    console.log('left: ' + charsLeft);
    console.log('limit: ' + limit);
    console.log(parts);
    // get full message length
    console.log('length: ' + parts.join('').length);
    // get full message
    console.log('message: ' + parts.join(''));
});
// output will be the same as above

To get counted data not in callback function, you can simply skip second parameter, in this case method will return object with the same fields:

var sms = SMS(),// also you can use "new" keyword
    text = 'Hello, world! This is a simple example of how to use "smscountjs" to calculate current total sms count and characters left to next sms. SMS in latin may contain up to 160 characters per SMS';

console.log(sms.count(text));

Output_5 See? It's pretty simple!

How about UTF-16?

smscount perfectly recognize 7-bit (latin), 8-bit (frehcn and german specific symbols) and 16-bit (cyrillic) charset according to GSM 03.38. Just put string with your encoding and get results. Also it works with mixed encodings (shortest limit will be used in this case):

var latinText = 'to have fun without stopping',
    frenchText = 's\'amuser sans arrêt',
    cyrillicText = 'веселиться без остановки',
    mixedText = latinText + ', ' + frenchText + ', ' + cyrillicText;

latinText.smsCount(function(totalCount, charsLeft, parts, limit) {
    console.log('limit: ' + limit);
});
frenchText.smsCount(function(totalCount, charsLeft, parts, limit) {
    console.log('limit: ' + limit);
});
cyrillicText.smsCount(function(totalCount, charsLeft, parts, limit) {
    console.log('limit: ' + limit);
});
mixedText.smsCount(function(totalCount, charsLeft, parts, limit) {
    console.log('limit: ' + limit);
});

Code above will output: Output_4

Methods

smscount has some methods, available only from object oriented uasge:

In action:

var sms = SMS(),  // also you can use "new" keyword
    text = 'Hello, world! This is a simple example of how to use "smscountjs" to calculate current total sms count and characters left to next sms. SMS in latin may contain up to 160 characters per SMS';

sms.count(text);
console.log('left: ' + sms.charsLeft());
console.log('limit: ' + sms.limit());
console.log('total: ' + sms.total());
console.log(sms.parts());

Configuration

You can configure limit according to 7-bit, 8-bit, 16-bit encoding, by passing options object to SMS constructor:

var sms = SMS({
        _7bit: 50
    }),// also you can use "new" keyword
    text = 'Hello, world! This is a simple example of how to use "smscountjs" to calculate current total sms count and characters left to next sms. SMS in latin may contain up to 160 characters per SMS';

sms.count(text, function(totalCount, charsLeft, parts, limit) {
    console.log('total: ' + totalCount);
    console.log('left: ' + charsLeft);
    console.log('limit: ' + limit);
    console.log(parts);
    // get full message length
    console.log('length: ' + parts.join('').length);
    // get full message
    console.log('message: ' + parts.join(''));
});

Code above will output: Output_3

To configure call from String object, pass options object as first parameter and callback as second parameter:

var text = 'Hello, world! This is a simple example of how to use "smscountjs" to calculate current total sms count and characters left to next sms. SMS in latin may contain 160 characters per SMS';

text.smsCount({
    _7bit: 50
}, function(totalCount, charsLeft, parts) {
    console.log('total: ' + totalCount);
    console.log('left: ' + charsLeft);
    console.log(parts)
});
// output will be the same as above

Thats all, again pretty simple, yeah?

References

License

Under MIT license (http://www.opensource.org/licenses/mit-license.php)