Skip to content

Instantly share code, notes, and snippets.

@zikani03
Last active August 29, 2015 14:16
Show Gist options
  • Save zikani03/93efcb71429ea377efc5 to your computer and use it in GitHub Desktop.
Save zikani03/93efcb71429ea377efc5 to your computer and use it in GitHub Desktop.
var cmd = require("cmd.js"),
truncate = require("./truncate");
cmd.module('truncate', truncate);
var articles = [
{ title: "Lorem ipsum dolor sit amet, musis nihilominus admonendus tu tu", body : "Lorem ipsum dolor sit amet, caelum levaverunt te finis puellam. Aderit omnia salva deus Hellenici silentio flebam terris classes nostram."},
{ title: "My first article", body: "Ego esse ait mea Christianis, concursus male nuptiarum civibus nescis. Quos ducem cum magna duobus discessit de me in deinde cupis ei."},
{ title: "My second article post", body : "Mansione sed eu fugiens laudo in, zurziaca in fuerat construeret cena. Nubila grata gratias eos est Apollonius in modo invenit nutricis actum in lucem."},
{ title: "This is just an article", body : "Filiam sunt amore tolle quae non potentiae cognoscitivarum non ait. Neminem habere matrem Domini in fuerat eum filiam rex Dionysiadi rex Dionysiadi rex in modo ad per sanctus primum subsannio."}
];
cmd.get('body').
and.truncate(30).
and.log(articles);
cmd.get('title').
and.truncate({size: 10}).
and.log(articles);
// truncate command for : https://github.com/NateFerrero/cmd.js
(function () {
'use strict';
this.export = function () {
/**
* Command: truncate(size)(String) - truncates a string
* Command: truncate({ size: Number})(String) - truncates a string
* @author Zikani Nyirenda Mwase
*/
this.each = function (args, val) {
var len = 10;
var _arg = args[0];
if (typeof _arg === 'number') {
len = _arg;
}
len = _arg.hasOwnProperty('size') ? _arg.size : len;
if (len >= val.length) {
len = val.length;
}
return val.substring(0, len) + '...';
};
};
}).call(typeof module === 'undefined' ? this['cmd:lib'].add = {} : this);
var cmd = require('cmd.js'),
truncate = require('./truncate'),
expect = require('chai').expect;
/**
* Add the command to the global cmd Command
*/
cmd.module('truncate', truncate);
/**
* Tests
*/
describe('cmd.truncate', function () {
it('is a function', function () {
expect(cmd.truncate).to.be.a('function');
});
it('truncates a string to the specified length', function () {
expect(cmd.truncate(2)("Hello")).to.deep.equal(["He..."]);
expect(cmd.truncate(3)("Hello")).to.deep.equal(["Hel..."]);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment