Skip to content

Instantly share code, notes, and snippets.

@oakfang
Created March 16, 2016 14:20
Show Gist options
  • Save oakfang/d1688b9fdf5a98460b8b to your computer and use it in GitHub Desktop.
Save oakfang/d1688b9fdf5a98460b8b to your computer and use it in GitHub Desktop.
Extract unicode hashtags from text
'use strict';
const twitter = require('twitter-text');
function extractTags(text) {
const tags = twitter.extractHashtagsWithIndices(text);
let buffer = '';
let i = 0;
for (let t of tags) {
buffer += text.substring(i, t.indices[0]);
i = t.indices[1];
}
buffer += text.substr(i);
return {
tags: tags.map(t => t.hashtag),
text: buffer.trim()
};
}
function tag(texts, vars) {
vars = vars || [];
let buffer = '';
for (let t of texts) {
buffer += t;
let v = vars.shift();
if (v) buffer += v;
}
return extractTags(buffer);
}
console.log(tag`שלום עולם! #ברכה #באנאלית`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment