Socket
Socket
Sign inDemoInstall

pipetteur

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pipetteur - npm Package Compare versions

Comparing version 0.0.1 to 1.0.0

.coveralls.yml

13

package.json
{
"name": "pipetteur",
"version": "0.0.1",
"version": "1.0.0",
"description": "A function to extract any colors from a string",
"main": "pipetteur.js",
"scripts": {
"test": "mocha"
"test": "npm run lint && mocha",
"lint": "jshint .",
"travis": "npm run lint && NODE_ENV=development ./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha -- --reporter dot && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
},

@@ -31,8 +33,13 @@ "repository": {

"dependencies": {
"css-color-names": "0.0.1"
"onecolor": "^2.4.0",
"synesthesia": "^1.0.0"
},
"devDependencies": {
"coveralls": "^2.11.1",
"istanbul": "^0.3.0",
"jshint": "^2.5.2",
"mocha": "^1.20.1",
"mocha-lcov-reporter": "0.0.1",
"unexpected": "^3.2.3"
}
}

@@ -1,65 +0,28 @@

var colorNames = require('css-color-names');
var synesthesia = require('synesthesia');
var color = require('onecolor');
var hexRegExp = /(?:^|\b)#(?:[0-9a-f]{6}|[0-9a-f]{3})(?:^|\b)/gi;
var channelRegExp = /\s*(\.\d+|\d+(?:\.\d+)?)(%)?\s*/,
alphaChannelRegExp = /\s*(\.\d+|\d+(?:\.\d+)?)\s*/,
cssColorRegExp = new RegExp(
'\\b(rgb|hsl|hsv)a?' +
'\\(' +
channelRegExp.source + ',' +
channelRegExp.source + ',' +
channelRegExp.source +
'(?:,' + alphaChannelRegExp.source + ')?' +
'\\)\\b', 'gi');
var colorMatcher = function (str) {
var pipetteur = function (str) {
if (typeof str !== 'string') {
throw new Error('pipeteur: Expected string input, got ' + typeof str);
throw new Error('pipetteur: Expected string input, got ' + typeof str);
}
var matches = [],
parts = str.split(' '),
hexmatch,
cssmatch;
match,
lines;
// Match named colors
parts.forEach(function (part, idx) {
if (part in colorNames) {
matches.push({
index: parts.slice(0, idx).join(' ').length,
match: part
});
}
});
// Match colors incrementally
while ((match = synesthesia.all.exec(str)) !== null) {
lines = str.slice(0, match.index).split('\n');
// Object.keys(colorNames).forEach(function (name) {
// var idx = str.indexOf(name);
// if (idx !== -1) {
// matches.push({
// index: idx,
// match: name
// });
// }
// });
// Match hex colors
while (hexmatch = hexRegExp.exec(str)) {
matches.push({
index: hexmatch.index,
match: hexmatch[0]
index: match.index,
line: lines.length,
column: lines[lines.length - 1].length + 1,
match: match[0],
color: color(match[0])
});
}
// Match CSS colors
while (cssmatch = cssColorRegExp.exec(str)) {
matches.push({
index: cssmatch.index,
match: cssmatch[0]
});
}
// Reset search indexes
hexRegExp.lastIndex = 0;
cssColorRegExp.lastIndex = 0;
synesthesia.all.lastIndex = 0;

@@ -69,7 +32,2 @@ return matches;

colorMatcher.regexp = {
hex: hexRegExp,
css: cssColorRegExp
};
module.exports = colorMatcher;
module.exports = pipetteur;

@@ -22,5 +22,9 @@ var expect = require('unexpected');

expect(matches, 'to have length', 1);
expect(matches[0], 'to have properties', ['index', 'match']);
expect(matches[0], 'to have properties', ['line', 'column', 'index', 'match', 'color']);
expect(matches[0].line, 'to be', 1);
expect(matches[0].column, 'to be', 1);
expect(matches[0].index, 'to be', 0);
expect(matches[0].match, 'to be', hex);
expect(matches[0].color.isColor, 'to be', true);
expect(matches[0].color.hex(), 'to be', hex.toLowerCase());
});

@@ -55,2 +59,154 @@

});
it('should match a set of valid 6-char hex substrings', function (done) {
var hexes = [
{
string: 'foo #000000 bar',
hex: '#000000',
index: 4
},
{
string: 'one, two, #FFFFFF, three',
hex: '#FFFFFF',
index: 10
},
{
string: 'hvid (#ffffff) er pænt',
hex: '#ffffff',
index: 6
},
{
string: '#123456 are numbers',
hex: '#123456',
index: 0
},
{
string: 'alphabet song: #abcdef',
hex: '#abcdef',
index: 15
},
{
string: '#alphab et #ABCDEF gehijkl',
hex: '#ABCDEF',
index: 11
},
{
string: 'background:#fedcba',
hex: '#fedcba',
index: 11
},
{
string: '$color=#FEDCBA',
hex: '#FEDCBA',
index: 7
}
];
expect(hexes, 'to be an array whose items satisfy', function (obj) {
var matches = pipetteur(obj.string);
expect(matches, 'to be a non-empty array');
expect(matches, 'to have length', 1);
expect(matches[0], 'to have properties', ['index', 'match']);
expect(matches[0].index, 'to be', obj.index);
expect(matches[0].match, 'to be', obj.hex);
});
done();
});
it('should match a set of valid 3-char hex substrings', function (done) {
var hexes = [
{
string: 'foo #000 bar',
hex: '#000',
index: 4
},
{
string: 'one, two, #FFF, three',
hex: '#FFF',
index: 10
},
{
string: 'hvid (#fff) er pænt',
hex: '#fff',
index: 6
},
{
string: '#123 are numbers',
hex: '#123',
index: 0
},
{
string: 'alphabet song: #abc',
hex: '#abc',
index: 15
},
{
string: '#alphab et #ABC gehijkl',
hex: '#ABC',
index: 11
},
{
string: 'background:#fed',
hex: '#fed',
index: 11
},
{
string: '$color=#FED',
hex: '#FED',
index: 7
}
];
expect(hexes, 'to be an array whose items satisfy', function (obj) {
var matches = pipetteur(obj.string);
expect(matches, 'to be a non-empty array');
expect(matches, 'to have length', 1);
expect(matches[0], 'to have properties', ['index', 'match']);
expect(matches[0].line, 'to be', 1);
expect(matches[0].column, 'to be', obj.index + 1);
expect(matches[0].index, 'to be', obj.index);
expect(matches[0].match, 'to be', obj.hex);
});
done();
});
it('should not match non-hex strings', function (done) {
var hexes = [
'#',
'#0',
'#00',
'#0000',
'#00000',
'#0000000'
];
expect(hexes, 'to be an array whose items satisfy', function (hex) {
var matches = pipetteur(hex);
expect(matches, 'to be an empty array');
});
done();
});
it('should match multiple colors in the same string', function (done) {
var strings = [
'#000000 #ffffff',
'#123456 and #234567',
'First: #123, Second: #fff000',
'Unlikely combination: #123#321'
];
expect(strings, 'to be an array whose items satisfy', function (str) {
var matches = pipetteur(str);
expect(matches, 'to have length', 2);
});
done();
});
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc