Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

iconize

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

iconize - npm Package Compare versions

Comparing version 0.0.1 to 0.1.0

165

main.js
// Generated by CoffeeScript 1.6.3
(function() {
var async, changeExt, defaultIconName, defaultOptions, extend, fs, genPngCss, genSvgCss, gm, iconizeAll, iconizeSingle, optimizeSvg, path, svgStrToUri, svgToPng, svgo, trycatch, url, _;
var async, baseNameNoExt, changeExt, defaultCssSelector, defaultOptions, extend, fs, genCss, iconizeAll, iconizeSingle, lowercaseBaseNameNoExt, optimizeSvg, path, pngName, svg2png, svgDims, svgStrToUri, svgToPng, svgo, temp, trycatch, xml2js;
_ = require('lodash');
async = require('async');

@@ -15,8 +13,10 @@

url = require('url');
svgo = require('svgo');
gm = require('gm');
svg2png = require('svg2png');
svgo = require('svgo');
temp = require('temp');
xml2js = require('xml2js');
trycatch = function(fn) {

@@ -30,10 +30,20 @@ try {

defaultIconName = function(filePath) {
var baseName, baseNameNoExt, extName;
baseNameNoExt = function(filePath) {
var baseName, ext;
baseName = path.basename(filePath);
extName = path.extname(filePath);
baseNameNoExt = baseName.slice(0, baseName.length - extName.length);
return baseNameNoExt.replace(/[^-_A-Za-z0-9]/g, '').toLowerCase();
ext = path.extname(filePath);
return baseName.slice(0, baseName.length - ext.length);
};
lowercaseBaseNameNoExt = function(filePath) {
return baseNameNoExt(filePath).replace(/[^-_A-Za-z0-9]/g, '').toLowerCase();
};
defaultCssSelector = function(svgPath, variant) {
var base, selector;
base = lowercaseBaseNameNoExt(svgPath);
selector = variant.selector || '';
return ".icon-" + base + selector;
};
changeExt = function(fromPath, toExt) {

@@ -58,18 +68,50 @@ var toPath;

genSvgCss = function(iconName, variant, opts) {
return "." + opts.cssPrefix + iconName + variant.selector + " {\n width: " + opts.width + "px;\n height: " + opts.height + "px;\n background-size: contain;\n background-image: url(" + (svgStrToUri(variant.optSvgStr)) + ");\n }";
pngName = function(svgPath, variant) {
var filePath;
filePath = baseNameNoExt(svgPath);
if (variant.name != null) {
filePath += "-" + variant.name;
}
return filePath += '.png';
};
genPngCss = function(iconName, variant, opts) {
var pngUrl;
pngUrl = url.resolve(opts.pngPrefix, variant.pngPath);
return "." + opts.cssPrefix + iconName + variant.selector + " {\n width: " + opts.width + "px;\n height: " + opts.height + "px;\n background-size: contain;\n background-image: url(" + pngUrl + ");\n}";
svgDims = function(svgStr, opts, done) {
return xml2js.parseString(svgStr, function(err, obj) {
var height, width, _ref, _ref1, _ref2, _ref3;
width = (obj != null ? (_ref = obj.svg) != null ? (_ref1 = _ref.$) != null ? _ref1.width : void 0 : void 0 : void 0) || opts.defaultWidth;
height = (obj != null ? (_ref2 = obj.svg) != null ? (_ref3 = _ref2.$) != null ? _ref3.width : void 0 : void 0 : void 0) || opts.defaultHeight;
return done(null, {
width: width,
height: height
});
});
};
svgToPng = function(svgStr, pngPath, opts, done) {
var svgBuf;
svgBuf = new Buffer(svgStr);
return gm(svgBuf).resize(opts.width, opts.height).write(pngPath, done);
genCss = function(svgPath, variant, opts, done) {
var pngUrl, sep;
sep = /\/$/.test(opts.cssUrlPrefix) ? '' : '/';
pngUrl = opts.cssUrlPrefix + sep + pngName(svgPath, variant);
return svgDims(variant.svgStr, opts, function(err, dims) {
var css;
css = "" + (opts.cssSelector(svgPath, variant)) + " {\n width: " + dims.width + "px;\n height: " + dims.height + "px;\n background-image: url(" + pngUrl + ");\n background-image: url(" + (svgStrToUri(variant.svgStr)) + "), none;\n}";
return done(null, css);
});
};
svgToPng = function(svgStr, pngPath, done) {
var tempPath;
tempPath = temp.path({
suffix: '.svg'
});
return fs.writeFile(tempPath, svgStr, function(err) {
if (err) {
return done(err);
}
return svg2png(tempPath, pngPath, function(err) {
fs.unlink(tempPath);
return done(err);
});
});
};
iconizeSingle = function(svgPath, opts, done) {

@@ -80,7 +122,6 @@ if (done == null) {

return fs.readFile(svgPath, function(err, svgBuf) {
var fn, iconName, iconVariants, o, svgStr;
var fn, iconVariants, svgStr;
if (err) {
return done(err);
}
iconName = opts.iconName(svgPath);
svgStr = svgBuf.toString();

@@ -95,42 +136,37 @@ iconVariants = opts.variants.map(function(v) {

iconVariants.unshift({
name: '',
selector: '',
name: null,
selector: null,
svgStr: svgStr
});
o = new svgo();
fn = function(v, done) {
return optimizeSvg(v.svgStr, function(err, optStr) {
v.optSvgStr = optStr;
v.svgStr = optStr;
return done(null);
});
};
return async.each(iconVariants, fn, function(err) {
var pngCss, svgCss;
return async.eachSeries(iconVariants, fn, function(err) {
var genCssFn;
if (err) {
return done(err);
}
iconVariants.forEach(function(v) {
var pngName;
pngName = iconName;
if (v.name !== '') {
pngName += "-" + v.name;
}
return v.pngPath = path.join(opts.pngDir, "" + pngName + ".png");
});
svgCss = iconVariants.map(function(v) {
return genSvgCss(iconName, v, opts);
});
svgCss = svgCss.join('\n');
pngCss = iconVariants.map(function(v) {
return genPngCss(iconName, v, opts);
});
pngCss = pngCss.join('\n');
fn = function(v, done) {
return svgToPng(v.svgStr, v.pngPath, opts, done);
genCssFn = function(v, done) {
return genCss(svgPath, v, opts, done);
};
return async.each(iconVariants, fn, function(err) {
return async.mapSeries(iconVariants, genCssFn, function(err, cssList) {
var css, genPngFn;
if (err) {
return done(err);
}
return done(null, [svgCss, pngCss]);
css = cssList.join('\n');
genPngFn = function(v, done) {
var pngPath;
pngPath = path.join(opts.pngDir, pngName(svgPath, v));
return svgToPng(v.svgStr, pngPath, done);
};
return async.eachSeries(iconVariants, genPngFn, function(err) {
if (err) {
return done(err);
}
return done(null, css);
});
});

@@ -147,4 +183,4 @@ });

opts = extend(defaultOptions, opts);
if (opts.pngPrefix == null) {
opts.pngPrefix = opts.pngDir;
if (opts.cssUrlPrefix == null) {
opts.cssUrlPrefix = opts.pngDir;
}

@@ -157,18 +193,9 @@ trycatch(function() {

};
return async.map(pathList, fn, function(err, results) {
var pngCss, svgCss, _ref;
return async.mapSeries(pathList, fn, function(err, cssList) {
var cssData;
if (err) {
return done(err);
}
_ref = _.zip(results), svgCss = _ref[0], pngCss = _ref[1];
svgCss = svgCss.join('\n');
pngCss = pngCss.join('\n');
try {
fs.writeFileSync(opts.svgCssPath, svgCss);
fs.writeFileSync(opts.pngCssPath, pngCss);
} catch (_error) {
err = _error;
return done(err);
}
return done(null);
cssData = cssList.join('\n');
return fs.writeFile(opts.cssPath, cssData, done);
});

@@ -178,9 +205,7 @@ };

defaultOptions = {
width: 32,
height: 32,
iconName: defaultIconName,
svgCssPath: 'svg-icons.css',
pngCssPath: 'png-icons.css',
defaultWidth: 32,
defaultHeight: 32,
cssPath: 'icons.css',
pngDir: 'png',
cssPrefix: 'icon-',
cssSelector: defaultCssSelector,
variants: []

@@ -187,0 +212,0 @@ };

{
"name": "iconize",
"version": "0.0.1",
"description": "Converts a bunch of SVG icons to CSS data URLs with PNG fallbacks",
"version": "0.1.0",
"description": "Converts a bunch of SVG icons to CSS data URLs with PNG fallbacks.",
"keywords": [

@@ -11,5 +11,5 @@ "icon",

"author": "Tiago Quelhas <tiagoq@gmail.com>",
"repository" : {
"type" : "git",
"url" : "http://github.com/tjgq/iconize"
"repository": {
"type": "git",
"url": "http://github.com/tjgq/iconize"
},

@@ -25,7 +25,9 @@ "homepage": "http://github.com/tjgq/iconize",

"async": "~0.2.10",
"gm": "~1.14.2",
"lodash": "~2.4.1",
"svgo": "~0.4.4",
"extend": "~1.2.1"
"extend": "~1.2.1",
"svg2png": "~1.0.1",
"temp": "~0.6.0",
"xml2js": "~0.4.1"
}
}

@@ -11,23 +11,23 @@ # iconize

* width: icon width (default: 32)
* *defaultWidth*: default icon width in pixels, when unspecified by the SVG file. The default is `32`.
* height: icon height (default: 32)
* *defaultHeight*: default icon height in pixels, when unspecified by the SVG file. The default is `32`.
* iconName: a function that takes a path to an svg file and returns an icon name suitable for inclusion in a filename and a CSS class name. Default: returns the basename of the file, without the extension, converted to lowercase and with all characters removed except letters, digits, hyphen and underscore.
* *cssPath*: path to generated CSS file. The default is `icons.css`.
* svgCssPath: path to generated CSS file, SVG version (default: `svg-icons.css`)
* *pngDir*: path to the directory where PNG files are placed, which will be created if nonexistent. The default is `png`.
* pngCssPath: path to generated CSS file, PNG version (default: `png-icons.css`)
* *cssUrlPrefix*: path to PNG files used by CSS URLs. By default, the value of *pngDir* is used.
* pngDir: path to the directory where PNG files are placed; will be created if nonexistent (default: `png`)
* *cssSelector*: a function taking an SVG path and a variant (see below) as arguments, and returning the CSS selector for the variant. The default is `.icon-#{base}#{selector}`, where
* pngPrefix: path to PNG files used by CSS URLs (default: same as pngDir)
* *base* is the basename of the SVG path, converted to lowercase, and with all characters other than letters, digits, hyphen and underscore removed.
* cssPrefix: prefix for CSS classes (default: `icon-`)
* *selector* is *variant.selector*.
* variants: a (possibly empty) array of objects with keys:
* name: variant name to be appended to PNG file names
* selector: CSS selector to be appended to CSS classes
* transform: a function taking a string and returning another string.
* *variants*: a (possibly empty) array of objects with keys:
* *name*: variant name to be appended to PNG file names.
* *selector*: string to be appended to the CSS selector for this variant.
* *transform*: a function taking an SVG string and returning another SVG string.
Variants allow the creation of alternative versions of the icons by transforming the SVG (e.g., change the color on hover).

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