Skip to content

Instantly share code, notes, and snippets.

@andersr
Created December 6, 2015 02:02
Show Gist options
  • Save andersr/bdd33f1486f6eeb10ab5 to your computer and use it in GitHub Desktop.
Save andersr/bdd33f1486f6eeb10ab5 to your computer and use it in GitHub Desktop.
LogdTags = {
getTags : function(str) {
var tags = [];
var hashTagPattern = /#[A-Za-z0-9_]*/gi;
tags = _.map(
// 1. find all matches for '#foo' in str
str.match(hashTagPattern),
// 2. remove first character (the '#')
function(tag) {
return tag.substr(1, tag.length);
}
);
return tags;
},
upsertTags: function(tags){
for(var i = 0; i < tags.length; i++){
var matching_tag = Tags.findOne({title: tags[i]});
if (!matching_tag){
var tagAttributes = {
title: tags[i],
authors: [{
authorId: Meteor.userId(),
lastUsed: new Date()
}]
};
Meteor.call('insertTag', tagAttributes, function(error, result){
if (error){
console.log(error.reason);
};
});
} else {
Meteor.call('updateTag', matching_tag._id, function(error, result){
if (error){
console.log(error.reason);
};
});
};
};
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment