Skip to content

Commit

Permalink
⚡ add cb option to use it as a module
Browse files Browse the repository at this point in the history
  • Loading branch information
watilde committed Apr 6, 2016
1 parent 3e0b477 commit 0f6b5d5
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/search.js
Expand Up @@ -6,7 +6,8 @@ var intersection = require('lodash.intersection')
var sortBy = require('lodash.sortby')
var random = 0

function search (words, options) {
function search (words, options, cb) {
cb = typeof cb === 'function' ? cb : false
var emojis = filter(emojilib, function (emoji, name) {
if (!emoji.char) return false
emoji.weight = intersection(emoji.keywords, words).length * -1
Expand All @@ -16,36 +17,37 @@ function search (words, options) {
})

if (emojis.length === 0) {
if (cb) return cb([])
console.log('Emoji not found 😿')
return
}

if (emojis.length === 1) {
if (cb) return cb([emojis[0].char])
return ncp.copy(emojis[0].char, function () {
console.log('Copied ' + emojis[0].char)
})
}

if (options.random) {
random = Math.floor(Math.random() * emojis.length)
if (cb) return cb([emojis[random].char])
return ncp.copy(emojis[random].char, function () {
console.log('Copied ' + emojis[random].char)
})
}

prompt(sortBy(emojis, 'weight'))
}

function prompt (emojis) {
var choices = emojis.map(function (emoji) {
emojis = sortBy(emojis, 'weight').map(function (emoji) {
return emoji.char
})

if (cb) return cb(emojis)

inquirer.prompt([{
type: 'list',
name: 'emoji',
message: 'Emojis:\n',
choices: choices
choices: emojis
}], function (o) {
ncp.copy(o.emoji, function () {
console.log('Copied ' + o.emoji)
Expand Down

0 comments on commit 0f6b5d5

Please sign in to comment.