Vulcan/packages/telescope-lib/lib/utils.js
Maxime Quandalle 94c6121d91 Improve jsHint consistency
This commit touch a lot of lines of code with the goal to be more
rigorous about JavaScript code conventions defined in the `.jshintrc`.

Some modification:

* Add a list of used global symbols in the corresponding section of
  `.jshintrc`
* Use local variables instead of global in a lot of places where the
  keyword `var` was mistakenly forgotten
* Add missing semi-colons after instructions
* Add new lines at the end of files
* Remove trailing whitespaces
* Use newer name of some Meteor APIs, eg `addFiles` instead of
  `add_files`
* Add missing `break` statements in `switch` blocks
* Use `===` instead of `==` and `!==` instead of `!=`
* Remove unused variables

This commit should also fix a few bugs due to this lack of rigor. One
example of that was the test `typeof navElements === "array"` that was
never true because in JavaScript, `typeof [] === "object"`, we
replaced this test by the `_.isArray` method provided by underscore.
It might also fix some potential collision related to global
variables.

There is still plenty of work until Telescope code base passes jsHint
validation, but at least this commit is a step in the right direction.
2015-05-01 18:38:27 +02:00

220 lines
5.9 KiB
JavaScript

/**
* The global namespace for Telescope utils.
* @namespace Telescope.utils
*/
Telescope.utils = {};
/**
* Returns the user defined site URL or Meteor.absoluteUrl
*/
Telescope.utils.getSiteUrl = function() {
return Settings.get('siteUrl', Meteor.absoluteUrl());
};
/**
* Convert a camelCase string to dash-separated string
* @param {String} str
*/
Telescope.utils.camelToDash = function(str) {
return str.replace(/\W+/g, '-').replace(/([a-z\d])([A-Z])/g, '$1-$2').toLowerCase();
};
/**
* Convert a dash separated string to camelCase.
* @param {String} str
*/
Telescope.utils.dashToCamel = function(str) {
return str.replace(/(\-[a-z])/g, function($1){return $1.toUpperCase().replace('-','');});
};
/**
* Convert a string to camelCase and remove spaces.
* @param {String} str
*/
Telescope.utils.camelCaseify = function(str) {
return this.dashToCamel(str.replace(' ', '-'));
};
/**
* Trim a sentence to a specified amount of words and append an ellipsis.
* @param {String} s - Sentence to trim.
* @param {Number} numWords - Number of words to trim sentence to.
*/
Telescope.utils.trimWords = function(s, numWords) {
var expString = s.split(/\s+/,numWords);
if(expString.length >= numWords)
return expString.join(" ")+"…";
return s;
};
/**
* Capitalize a string.
* @param {String} str
*/
Telescope.utils.capitalise = function(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
};
Telescope.utils.getCurrentTemplate = function() {
var template = Router.current().lookupTemplate();
// on postsDaily route, template is a function
if (typeof template === "function") {
return template();
} else {
return template;
}
};
Telescope.utils.t = function(message) {
var d = new Date();
console.log("### "+message+" rendered at "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds());
};
Telescope.utils.nl2br = function(str) {
var breakTag = '<br />';
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
};
Telescope.utils.scrollPageTo = function(selector) {
$('body').scrollTop($(selector).offset().top);
};
Telescope.utils.getDateRange = function(pageNumber) {
var now = moment(new Date());
var dayToDisplay=now.subtract(pageNumber-1, 'days');
var range={};
range.start = dayToDisplay.startOf('day').valueOf();
range.end = dayToDisplay.endOf('day').valueOf();
// console.log("after: ", dayToDisplay.startOf('day').format("dddd, MMMM Do YYYY, h:mm:ss a"));
// console.log("before: ", dayToDisplay.endOf('day').format("dddd, MMMM Do YYYY, h:mm:ss a"));
return range;
};
// Telescope.utils.getPostCategories = function(post){
// var postCategories = _.map(post.categories, function(categoryId){
// return Categories.findOne(categoryId);
// });
// // put resulting array through a filter to remove empty values in case
// // some of the post's categories weren't found in the database
// return _.filter(postCategories, function(e){return e});
// }
//////////////////////////
// URL Helper Functions //
//////////////////////////
// This function should only ever really be necessary server side
// Client side using .path() is a better option since it's relative
// and shouldn't care about the siteUrl.
Telescope.utils.getRouteUrl = function(routeName, params, options) {
options = options || {};
var route = Router.url(
routeName,
params || {},
options
);
return route;
};
Telescope.utils.getSignupUrl = function() {
return this.getRouteUrl('atSignUp');
};
Telescope.utils.getSigninUrl = function() {
return this.getRouteUrl('atSignIn');
};
Telescope.utils.getPostUrl = function(id) {
return this.getRouteUrl('post_page', {_id: id});
};
Telescope.utils.getPostEditUrl = function(id) {
return this.getRouteUrl('post_edit', {_id: id});
};
Telescope.utils.getCommentUrl = function(id) {
return this.getRouteUrl('comment_reply', {_id: id});
};
Telescope.utils.getPostCommentUrl = function(postId, commentId) {
// get link to a comment on a post page
return this.getRouteUrl('post_page_comment', {
_id: postId,
commentId: commentId
});
};
Telescope.utils.slugify = function(text) {
if(text){
text = text.replace(/[^-_a-zA-Z0-9,&\s]+/ig, '');
text = text.replace(/\s/gi, "-");
text = text.toLowerCase();
}
return text;
};
Telescope.utils.getShortUrl = function(post) {
return post.shortUrl || post.url;
};
Telescope.utils.getDomain = function(url) {
var urlObject = Npm.require('url');
return urlObject.parse(url).hostname.replace('www.', '');
};
Telescope.utils.invitesEnabled = function() {
return Settings.get("requireViewInvite") || Settings.get("requirePostInvite");
};
/////////////////////////////
// String Helper Functions //
/////////////////////////////
Telescope.utils.cleanUp = function(s) {
return this.stripHTML(s);
};
Telescope.utils.sanitize = function(s) {
// console.log('// before sanitization:')
// console.log(s)
if(Meteor.isServer){
s = sanitizeHtml(s, {
allowedTags: [
'h3', 'h4', 'h5', 'h6', 'blockquote', 'p', 'a', 'ul',
'ol', 'nl', 'li', 'b', 'i', 'strong', 'em', 'strike',
'code', 'hr', 'br', 'div', 'table', 'thead', 'caption',
'tbody', 'tr', 'th', 'td', 'pre', 'img'
]
});
// console.log('// after sanitization:')
// console.log(s)
}
return s;
};
Telescope.utils.stripHTML = function(s) {
return s.replace(/<(?:.|\n)*?>/gm, '');
};
Telescope.utils.stripMarkdown = function(s) {
var html_body = marked(s);
return stripHTML(html_body);
};
// http://stackoverflow.com/questions/2631001/javascript-test-for-existence-of-nested-object-key
Telescope.utils.checkNested = function(obj /*, level1, level2, ... levelN*/) {
var args = Array.prototype.slice.call(arguments);
obj = args.shift();
for (var i = 0; i < args.length; i++) {
if (!obj.hasOwnProperty(args[i])) {
return false;
}
obj = obj[args[i]];
}
return true;
};
Telescope.log = function (s) {
if(Settings.get('debug', false))
console.log(s);
};