Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

fixed example and utilized class

  • Loading branch information...
commit 4c9e308d8acf3460731d5b04d1301e0d66fa42f4 1 parent 4d081b2
Nick Baugh authored
5  Readme.md
Source Rendered
@@ -78,12 +78,17 @@ Output in table format on command line:
78 78
 
79 79
 ## Dependency
80 80
 
  81
+1. Install npm module:
  82
+
81 83
 ```bash
82 84
 cd ~/project
83 85
 npm install pantone
84 86
 ```
85 87
 
  88
+2. Require and lookup color:
  89
+
86 90
 ```js
  91
+
87 92
 var pantone = require('pantone')
88 93
 
89 94
 // rgb usage
20  example.js
... ...
@@ -0,0 +1,20 @@
  1
+
  2
+var pantone = require('./pantone')
  3
+
  4
+// rgb usage
  5
+
  6
+pantone({ r: 42, g: 112, b: 174 }, rgbCallback)
  7
+
  8
+function rgbCallback(err, results) {
  9
+  if (err) throw new Error(err)
  10
+  console.log(results)
  11
+}
  12
+
  13
+// hex usage
  14
+
  15
+pantone({ hex: '2A70AE' }, hexCallback) // you can also use `rgb` instead of `hex`
  16
+
  17
+function hexCallback(err, results) {
  18
+  if (err) throw new Error(err)
  19
+  console.log(results)
  20
+}
97  pantone.js
... ...
@@ -1,10 +1,9 @@
1 1
 
2 2
 // # pantone
3 3
 
4  
-var request     = require('request')
5  
-  , cheerio     = require('cheerio')
6  
-  , querystring = require('querystring')
7  
-  , _           = require('underscore')
  4
+var request = require('request')
  5
+  , cheerio = require('cheerio')
  6
+  , _       = require('underscore')
8 7
 
9 8
 // Optiman's query url
10 9
 var url = 'http://www.netfront.fr/Services/rgb2pantone/pantone.htm' // ?r=0&g=0&b=0&rgb=000000'
@@ -17,75 +16,83 @@ var defaults = {
17 16
   , rgb : ''
18 17
 }
19 18
 
20  
-// globals to reduce callback nesting
21  
-var options, callback
22  
-
23 19
 // main function with callback
24  
-function pantone(_options, _callback) {
  20
+function Pantone(options, callback) {
  21
+
  22
+  var self = this
25 23
 
26  
-  options  = _options
27  
-  callback = _callback
  24
+  self.options  = options
  25
+  self.callback = callback
  26
+  self.rows     = []
28 27
 
29 28
   // check types
30  
-  if (typeof options !== 'object')
  29
+  if (typeof self.options !== 'object')
31 30
     return callback(new Error('pantone - `options` is not an object'))
32 31
 
33  
-  if (typeof callback !== 'function')
  32
+  if (typeof self.callback !== 'function')
34 33
     throw new Error('pantone - `callback` is not a function')
35 34
 
36 35
   // if user passed options.hex convert it to options.rgb
37  
-  if ('hex' in options) {
38  
-    if (typeof options.hex === 'string') {
39  
-      options.rgb = options.hex
  36
+  if ('hex' in self.options) {
  37
+    if (typeof self.options.hex === 'string') {
  38
+      self.options.rgb = self.options.hex
40 39
     }
41  
-    delete options.hex
  40
+    delete self.options.hex
42 41
   }
43 42
 
44 43
   // set defaults
45  
-  options = _.defaults(options, defaults)
46  
-
47  
-  // prepare querystring
48  
-  var qs = querystring.stringify(options)
  44
+  self.options = _.defaults(self.options, defaults)
49 45
 
50 46
   // send request
51  
-  request(url + '?' + qs, sendRequest)
  47
+  request({ url: url, qs: self.options }, sendRequest(self))
52 48
 
53 49
 }
54 50
 
55  
-var rows = []
  51
+function sendRequest(self) {
56 52
 
57  
-function sendRequest(err, res, body) {
  53
+  return function(err, res, body) {
58 54
 
59  
-  if (err) {
60  
-    err.message = err.message + '\nPlease visit https://github.com/teelaunch/pms-pantone-color-chart/ to report issues.'
61  
-    return callback(err)
62  
-  }
  55
+    if (err) {
  56
+      err.message = err.message + '\nPlease visit https://github.com/teelaunch/pms-pantone-color-chart/ to report issues.'
  57
+      return self.callback(err)
  58
+    }
  59
+
  60
+    var $ = cheerio.load(body)
63 61
 
64  
-  var $ = cheerio.load(body)
  62
+    if ($('#content table').length === 0)
  63
+      return self.callback(new Error('pantone - color match not available for your request'))
65 64
 
66  
-  if ($('#content table').length === 0)
67  
-    return callback(new Error('pantone - color match not available for your request'))
  65
+    $('#content table tr').each(parseTable(self));
68 66
 
69  
-  $('#content table tr').each(parseTable);
  67
+    // remove header row
  68
+    self.rows = self.rows.slice(2, self.rows.length)
70 69
 
71  
-  // remove header row
72  
-  rows = rows.slice(2, rows.length)
  70
+    self.callback(null, self.rows)
73 71
 
74  
-  callback(null, rows)
  72
+  }
  73
+
  74
+}
  75
+
  76
+function parseTable(self) {
  77
+
  78
+  return function(i,tr) {
  79
+    var $   = require('cheerio')
  80
+      , $td = $(tr).find('td')
  81
+    self.rows.push({
  82
+        dist : parseFloat($td.eq(0).text())
  83
+      , name : $td.eq(1).text()
  84
+      , r    : parseInt($td.eq(2).text(), 10)
  85
+      , g    : parseInt($td.eq(3).text(), 10)
  86
+      , b    : parseInt($td.eq(4).text(), 10)
  87
+      , hex  : $td.eq(5).text().substr(1)
  88
+    })
  89
+  }
75 90
 
76 91
 }
77 92
 
78  
-function parseTable(i,tr) {
79  
-  var $   = require('cheerio')
80  
-    , $td = $(tr).find('td')
81  
-  rows.push({
82  
-      dist : $td.eq(0).text()
83  
-    , name : $td.eq(1).text()
84  
-    , r    : parseInt($td.eq(2).text(), 10)
85  
-    , g    : parseInt($td.eq(3).text(), 10)
86  
-    , b    : parseInt($td.eq(4).text(), 10)
87  
-    , hex  : $td.eq(5).text()
88  
-  })
  93
+function pantone(options, callback) {
  94
+  var p = new Pantone(options, callback)
  95
+  return p
89 96
 }
90 97
 
91 98
 module.exports = pantone

0 notes on commit 4c9e308

Please sign in to comment.
Something went wrong with that request. Please try again.