color-name-list 9.9.0 → 9.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "color-name-list",
3
- "version": "9.9.0",
3
+ "version": "9.11.0",
4
4
  "description": "long list of color names",
5
5
  "main": "dist/colornames.json",
6
6
  "browser": "dist/colornames.umd.js",
@@ -33,7 +33,7 @@
33
33
  "homepage": "https://github.com/meodai/color-names#readme",
34
34
  "devDependencies": {
35
35
  "closestvector": "^0.6.0",
36
- "color-name-lists": "^1.1.0",
36
+ "color-name-lists": "^2.1.0",
37
37
  "commitizen": "^4.2.4",
38
38
  "eslint": "^5.15.1",
39
39
  "eslint-config-google": "^0.10.0",
package/scripts/server.js CHANGED
@@ -24,10 +24,11 @@ const responseHeaderObj = {
24
24
  'Access-Control-Allow-Credentials': false,
25
25
  'Access-Control-Max-Age': '86400',
26
26
  'Access-Control-Allow-Headers': 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept',
27
- 'Content-Encoding': 'gzip',
28
27
  'Content-Type': 'application/json; charset=utf-8',
29
28
  };
30
29
 
30
+ // accepts encoding
31
+
31
32
  // [{name: 'red', value: '#f00'}, ...]
32
33
  const colorsLists = {
33
34
  default: colors,
@@ -35,7 +36,7 @@ const colorsLists = {
35
36
  bestOf: colorsBestOf,
36
37
  };
37
38
 
38
- Object.assign(colorsLists, colorNameLists);
39
+ Object.assign(colorsLists, colorNameLists.lists);
39
40
 
40
41
  const avalibleColorNameLists = Object.keys(colorsLists);
41
42
 
@@ -56,12 +57,23 @@ const validateColor = (color) => (
56
57
  * @param {object} responseObj the actual response object
57
58
  * @param {*} statusCode HTTP status code
58
59
  */
59
- const httpRespond = (response, responseObj = {}, statusCode = 200) => {
60
- response.writeHead(statusCode, responseHeaderObj);
61
- // ends the response with the gziped API answer
62
- zlib.gzip(JSON.stringify(responseObj), (_, result) => {
63
- response.end(result);
64
- });
60
+ const httpRespond = (
61
+ response,
62
+ responseObj = {},
63
+ statusCode = 200,
64
+ responseHeader = responseHeaderObj
65
+ ) => {
66
+ response.writeHead(statusCode, responseHeader);
67
+ const stringifiedResponse = JSON.stringify(responseObj);
68
+
69
+ if (responseHeader['Content-Encoding'] === 'gzip') {
70
+ // ends the response with the gziped API answer
71
+ zlib.gzip(stringifiedResponse, (_, result) => {
72
+ response.end(result);
73
+ });
74
+ } else {
75
+ response.end(stringifiedResponse);
76
+ }
65
77
  };
66
78
 
67
79
  const respondNameSearch = (
@@ -69,7 +81,8 @@ const respondNameSearch = (
69
81
  listKey = 'default',
70
82
  requestUrl,
71
83
  request,
72
- response
84
+ response,
85
+ responseHeader,
73
86
  ) => {
74
87
  const nameQuery = request.url.replace(requestUrl.search, '')
75
88
  // splits the base url from everything
@@ -83,15 +96,20 @@ const respondNameSearch = (
83
96
  const searchString = decodeURI(nameString || nameQuery);
84
97
 
85
98
  if (searchString.length < 3) {
86
- return httpRespond(response, {error: {
87
- status: 404,
88
- message: `the color name your are looking for must be at least 3 characters long.`,
89
- }}, 404);
99
+ return httpRespond(
100
+ response,
101
+ {error: {
102
+ status: 404,
103
+ message: `the color name your are looking for must be at least 3 characters long.`,
104
+ }},
105
+ 404,
106
+ responseHeader
107
+ );
90
108
  }
91
109
 
92
110
  return httpRespond(response, {
93
111
  colors: findColors.searchNames(searchString, listKey),
94
- }, 200);
112
+ }, 200, responseHeader);
95
113
  };
96
114
 
97
115
  const respondValueSearch = (
@@ -99,7 +117,8 @@ const respondValueSearch = (
99
117
  listKey = 'default',
100
118
  requestUrl,
101
119
  request,
102
- response
120
+ response,
121
+ responseHeader
103
122
  ) => {
104
123
  const uniqueMode = searchParams.has('noduplicates')
105
124
  && searchParams.get('noduplicates') === 'true';
@@ -123,10 +142,17 @@ const respondValueSearch = (
123
142
  ));
124
143
 
125
144
  if (invalidColors.length) {
126
- return httpRespond(response, {error: {
127
- status: 404,
128
- message: `'${invalidColors.join(', ')}' is not a valid HEX color`,
129
- }}, 404);
145
+ return httpRespond(
146
+ response,
147
+ {
148
+ error: {
149
+ status: 404,
150
+ message: `'${invalidColors.join(', ')}' is not a valid HEX color`,
151
+ }
152
+ },
153
+ 404,
154
+ responseHeader
155
+ );
130
156
  }
131
157
 
132
158
  let paletteTitle;
@@ -155,7 +181,7 @@ const respondValueSearch = (
155
181
  return httpRespond(response, {
156
182
  paletteTitle,
157
183
  colors: colorsResponse,
158
- }, 200);
184
+ }, 200, responseHeader);
159
185
  };
160
186
 
161
187
  /**
@@ -173,6 +199,7 @@ const requestHandler = (request, response) => {
173
199
  const requestUrl = url.parse(request.url);
174
200
  const isAPI = requestUrl.pathname.includes(baseUrl);
175
201
  const isNamesAPI = requestUrl.pathname.includes(urlNameSubpath + '/');
202
+ const responseHeader = {...responseHeaderObj};
176
203
 
177
204
  // understanding where requests come from
178
205
  console.info(
@@ -180,12 +207,28 @@ const requestHandler = (request, response) => {
180
207
  request.headers.origin
181
208
  );
182
209
 
210
+ if (request.headers['accept-encoding']) {
211
+ const accepts = request.headers['accept-encoding'];
212
+ if (accepts.toLowerCase().includes("gzip")) {
213
+ responseHeader['Content-Encoding'] = 'gzip';
214
+ }
215
+ }
216
+
217
+ let accpets = request.headers['accept-encoding'];
218
+
183
219
  // makes sure the API is beeing requested
184
220
  if (!isAPI) {
185
- return httpRespond(response, {error: {
186
- status: 404,
187
- message: 'invalid URL: make sure to provide the API version',
188
- }}, 404);
221
+ return httpRespond(
222
+ response,
223
+ {
224
+ error: {
225
+ status: 404,
226
+ message: 'invalid URL: make sure to provide the API version',
227
+ }
228
+ },
229
+ 404,
230
+ responseHeader
231
+ );
189
232
  }
190
233
 
191
234
  // const search = requestUrl.search || '';
@@ -203,10 +246,16 @@ const requestHandler = (request, response) => {
203
246
  const isValidListKey = listKey && avalibleColorNameLists.includes(listKey);
204
247
 
205
248
  if (!isValidListKey) {
206
- return httpRespond(response, {error: {
207
- status: 404,
208
- message: `invalid list key: '${listKey}, available keys are: ${avalibleColorNameLists.join(', ')}`,
209
- }}, 404);
249
+ return httpRespond(
250
+ response,
251
+ {
252
+ error: {
253
+ status: 404,
254
+ message: `invalid list key: '${listKey}, available keys are: ${avalibleColorNameLists.join(', ')}`,
255
+ }
256
+ },
257
+ 404
258
+ );
210
259
  }
211
260
 
212
261
  if (!isNamesAPI) {
@@ -215,7 +264,8 @@ const requestHandler = (request, response) => {
215
264
  listKey,
216
265
  requestUrl,
217
266
  request,
218
- response
267
+ response,
268
+ responseHeader,
219
269
  );
220
270
  } else {
221
271
  return respondNameSearch(
@@ -223,7 +273,8 @@ const requestHandler = (request, response) => {
223
273
  listKey,
224
274
  requestUrl,
225
275
  request,
226
- response
276
+ response,
277
+ responseHeader,
227
278
  );
228
279
  }
229
280
  };
@@ -1,29 +1,31 @@
1
1
  const fs = require('fs');
2
2
 
3
3
  //const colors = fs.readFileSync('refs.csv').toString().split(`\n`);
4
- const colorsSRCfile = fs.readFileSync(__dirname + '/../../historic-master.json', 'utf8');
4
+ //const colorsSRCfile = fs.readFileSync(__dirname + '/../../historic-master.json', 'utf8');
5
+ const colorsSRCfile = fs.readFileSync(__dirname + '/json.json', 'utf8');
5
6
 
6
- const colors = eval(colorsSRCfile);
7
+ const colors = JSON.parse(colorsSRCfile);
7
8
 
8
- console.log(colors)
9
9
  const namedColors = JSON.parse(
10
- fs.readFileSync(__dirname + '/../../dist/colornames.json', 'utf8')
10
+ fs.readFileSync(__dirname + '/../../dist/colornames.json', 'utf8')
11
11
  );
12
12
 
13
13
  const uniqueColors = [];
14
14
 
15
- for(const color in colors) {
16
- let name = colors[color]['Name'];
15
+ colors.forEach((color) => {
16
+ let name = color['name'].replace(/Gray/g, 'Grey');
17
17
  const foundMath = namedColors.find(item => {
18
- return item.name.toLowerCase() === name.toLowerCase()
19
- || item.name.toLowerCase() === name.toLowerCase().split(' ').reverse().join(' ');
18
+ return item.name.toLowerCase() === name.toLowerCase() ||
19
+ item.name.toLowerCase() === name.toLowerCase().split(' ').reverse().join(' ') ||
20
+ item.name.toLowerCase() === name.toLowerCase().split(' ').join('-') ||
21
+ item.name.toLowerCase() === name.toLowerCase().split('-').join(' ');
20
22
  });
21
23
  if (!foundMath) {
22
24
  uniqueColors.push([
23
25
  name,
24
- '#' + colors[color]['HexaDecimal'].toLowerCase()
25
- ])
26
+ color['hex'].toLowerCase()
27
+ ]);
26
28
  }
27
- }
29
+ });
28
30
 
29
31
  fs.writeFileSync('uniques.csv', 'name,hex\n' + uniqueColors.join(`\n`));