Skip to content

Commit

Permalink
Merge branch 'master' into update-js-yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
XhmikosR committed Mar 8, 2021
2 parents ad126cf + b7602ac commit 475069f
Show file tree
Hide file tree
Showing 25 changed files with 118 additions and 168 deletions.
1 change: 1 addition & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"curly": true,
"eqeqeq": true,
"esversion": 6,
"immed": true,
"latedef": true,
"newcap": true,
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# svg-sprite [![npm version][npm-image]][npm-url] [![npm downloads][npm-downloads]][npm-url] [![Build Status][ci-image]][ci-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency Status][depstat-image]][depstat-url] [![Development Dependency Status][devdepstat-image]][devdepstat-url]
# svg-sprite

[![npm version][npm-image]][npm-url] [![npm downloads][npm-downloads]][npm-url] [![Build Status][ci-image]][ci-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency Status][depstat-image]][depstat-url] [![Development Dependency Status][devdepstat-image]][devdepstat-url]

svg-sprite is a low-level [Node.js](https://nodejs.org/) module that **takes a bunch of [SVG](https://www.w3.org/TR/SVG/) files**, optimizes them and bakes them into **SVG sprites** of several types:

Expand Down
2 changes: 1 addition & 1 deletion bin/svg-sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function addOption(name, option) {
yargs = yargs.describe(alias, option.description);

if ('default' in option) {
var template = (name.substr(-9) === '-template'),
var template = name.endsWith('-template'),
def = template ? path.resolve(path.dirname(__dirname), option.default) : option.default;
yargs = yargs.default(alias, def);

Expand Down
4 changes: 3 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# svg-sprite [![npm version][npm-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency Status][depstat-image]][depstat-url] [![Development Dependency Status][devdepstat-image]][devdepstat-url]
# svg-sprite

[![npm version][npm-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency Status][depstat-image]][depstat-url] [![Development Dependency Status][devdepstat-image]][devdepstat-url]

This file is part of the documentation of *svg-sprite* — a free low-level Node.js module that **takes a bunch of SVG files**, optimizes them and creates **SVG sprites** of several types. The package is [hosted on GitHub](https://github.com/svg-sprite/svg-sprite).

Expand Down
4 changes: 3 additions & 1 deletion docs/command-line.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# svg-sprite [![npm version][npm-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency Status][depstat-image]][depstat-url] [![Development Dependency Status][devdepstat-image]][devdepstat-url]
# svg-sprite

[![npm version][npm-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency Status][depstat-image]][depstat-url] [![Development Dependency Status][devdepstat-image]][devdepstat-url]

This file is part of the documentation of *svg-sprite* — a free low-level Node.js module that **takes a bunch of SVG files**, optimizes them and creates **SVG sprites** of several types. The package is [hosted on GitHub](https://github.com/svg-sprite/svg-sprite).

Expand Down
4 changes: 3 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# svg-sprite [![npm version][npm-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency Status][depstat-image]][depstat-url] [![Development Dependency Status][devdepstat-image]][devdepstat-url]
# svg-sprite

[![npm version][npm-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency Status][depstat-image]][depstat-url] [![Development Dependency Status][devdepstat-image]][devdepstat-url]

This file is part of the documentation of *svg-sprite* — a free low-level Node.js module that **takes a bunch of SVG files**, optimizes them and creates **SVG sprites** of several types. The package is [hosted on GitHub](https://github.com/svg-sprite/svg-sprite).

Expand Down
4 changes: 3 additions & 1 deletion docs/grunt-gulp.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# svg-sprite [![npm version][npm-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency Status][depstat-image]][depstat-url] [![Development Dependency Status][devdepstat-image]][devdepstat-url]
# svg-sprite

[![npm version][npm-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency Status][depstat-image]][depstat-url] [![Development Dependency Status][devdepstat-image]][devdepstat-url]

This file is part of the documentation of *svg-sprite* — a free low-level Node.js module that **takes a bunch of SVG files**, optimizes them and creates **SVG sprites** of several types. The package is [hosted on GitHub](https://github.com/svg-sprite/svg-sprite).

Expand Down
4 changes: 3 additions & 1 deletion docs/meta-data.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# svg-sprite [![npm version][npm-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency Status][depstat-image]][depstat-url] [![Development Dependency Status][devdepstat-image]][devdepstat-url]
# svg-sprite

[![npm version][npm-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency Status][depstat-image]][depstat-url] [![Development Dependency Status][devdepstat-image]][devdepstat-url]

This file is part of the documentation of *svg-sprite* — a free low-level Node.js module that **takes a bunch of SVG files**, optimizes them and creates **SVG sprites** of several types. The package is [hosted on GitHub](https://github.com/svg-sprite/svg-sprite).

Expand Down
4 changes: 3 additions & 1 deletion docs/shape-alignment.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# svg-sprite [![npm version][npm-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency Status][depstat-image]][depstat-url] [![Development Dependency Status][devdepstat-image]][devdepstat-url]
# svg-sprite

[![npm version][npm-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency Status][depstat-image]][depstat-url] [![Development Dependency Status][devdepstat-image]][devdepstat-url]

This file is part of the documentation of *svg-sprite* — a free low-level Node.js module that **takes a bunch of SVG files**, optimizes them and creates **SVG sprites** of several types. The package is [hosted on GitHub](https://github.com/svg-sprite/svg-sprite).

Expand Down
4 changes: 3 additions & 1 deletion docs/templating.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# svg-sprite [![npm version][npm-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency Status][depstat-image]][depstat-url] [![Development Dependency Status][devdepstat-image]][devdepstat-url]
# svg-sprite

[![npm version][npm-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency Status][depstat-image]][depstat-url] [![Development Dependency Status][devdepstat-image]][devdepstat-url]

This file is part of the documentation of *svg-sprite* — a free low-level Node.js module that **takes a bunch of SVG files**, optimizes them and creates **SVG sprites** of several types. The package is [hosted on GitHub](https://github.com/svg-sprite/svg-sprite).

Expand Down
14 changes: 7 additions & 7 deletions lib/svg-sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ SVGSpriter.prototype.add = function (file, name, svg) {
if (!svg.length) {
error = 'SVGSpriter.add: You must provide SVG contents';
}
if (file.substr(-name.length) !== name) {
if (!file.endsWith(name)) {
error = util.format('SVGSpriter.add: "%s" is not the local part of "%s"', name, file);
}
}
Expand All @@ -128,7 +128,7 @@ SVGSpriter.prototype.add = function (file, name, svg) {

// Instantiate a vinyl file
file = new File({
base: file.substring(0, file.length - name.length),
base: path.dirname(file),
path: file,
contents: Buffer.from(svg)
});
Expand Down Expand Up @@ -243,8 +243,8 @@ SVGSpriter.prototype._compile = function () {

// Else
} else {
var masterShapes = _.reject(this._shapes, function (shape) {
return !!shape.master;
var masterShapes = this._shapes.filter(function (shape) {
return !shape.master;
}).length;
this.info('Compiling %d shapes ...', masterShapes);

Expand All @@ -258,8 +258,8 @@ SVGSpriter.prototype._compile = function () {
this._shapes = this._shapes.sort(this.config.shape.sort);

// Set the shape namespaces on all master shapes
_.reject(this._shapes, function (shape) {
return !!shape.master;
this._shapes.filter(function (shape) {
return !shape.master;
}).map(function (shape, index) {
shape.setNamespace(this._indexNamespace(index));
}, this);
Expand Down Expand Up @@ -320,7 +320,7 @@ SVGSpriter.prototype._layout = function (config, cb) {
}

async.parallelLimit(tasks, this._limit, function (error, data) {
cb(error, files, _.zipObject(_.map(data, 'key'), data));
cb(error, files, _.zipObject(Object.keys(data).map(key => data[key].key), data));
});
};

Expand Down
16 changes: 3 additions & 13 deletions lib/svg-sprite/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function SVGSpriterConfig(config) {

this.log.debug('Prepared general options');

this.shape = ('shape' in config) ? _.assign({}, config.shape || {}) : {};
this.shape = ('shape' in config) ? Object.assign({}, config.shape || {}) : {};

var stat, t, transforms = null;

Expand Down Expand Up @@ -226,16 +226,6 @@ function SVGSpriterConfig(config) {
transforms = this.shape.transform;
}

// Alternatively use deprecated top-level transforms
// TODO: Remove in future version
if ('transform' in config) {
this.log.warn('The top-level `transform` option is deprecated and will be removed in a future version. Please use `shape.transform` instead.');

if ((transforms === null) && Array.isArray(config.transform)) {
transforms = config.transform;
}
}

// Fallback: Use default transformations
if (transforms === null) {
transforms = defaultShapeTransform;
Expand Down Expand Up @@ -266,7 +256,7 @@ function SVGSpriterConfig(config) {
this.log.debug('Prepared `shape` options');

this.svg = _.clone(defaultSVGConfig);
this.svg = ('svg' in config) ? _.assign(this.svg, config.svg || {}) : this.svg;
this.svg = ('svg' in config) ? Object.assign(this.svg, config.svg || {}) : this.svg;
this.svg.xmlDeclaration = this.svg.xmlDeclaration || false;
this.svg.doctypeDeclaration = this.svg.doctypeDeclaration || false;
this.svg.dimensionAttributes = this.svg.dimensionAttributes || false;
Expand Down Expand Up @@ -294,7 +284,7 @@ function SVGSpriterConfig(config) {

this.log.debug('Prepared `mode` options');

this.variables = _.extend({}, config.variables);
this.variables = Object.assign({}, config.variables);

this.log.debug('Prepared `variables` options');

Expand Down
9 changes: 5 additions & 4 deletions lib/svg-sprite/layouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ defaultVariables = {
classname : function() {
return function(str, render) {
var classname = render(str).replace(/\s+/g, ' ').split(' ').pop();
return (classname.indexOf('.') === 0) ? classname.substr(1) : classname;
return classname.startsWith('.') ? classname.substr(1) : classname;
};
},
escape : function() {
Expand All @@ -96,9 +96,10 @@ function SVGSpriteLayouter(spriter, config) {
this.mode = null;
this.files = {};
this.data = {};
this._commonData = _.extend({shapes: []}, defaultVariables, this._spriter.config.variables);
this._commonData = Object.assign({shapes: []}, defaultVariables, this._spriter.config.variables);

// Register the common shapes data
var shapesLength = this._spriter._shapes.length;
this._spriter._shapes.forEach(function(shape, index) {
var dimensions = shape.getDimensions(),
padding = shape.config.spacing.padding;
Expand All @@ -115,8 +116,8 @@ function SVGSpriteLayouter(spriter, config) {
inner : dimensions.height - padding.top - padding.bottom,
outer : dimensions.height
},
first : !index,
last : (index === (this._spriter._shapes.length - 1))
first : index === 0,
last : index === shapesLength - 1
});
}, this);

Expand Down
10 changes: 5 additions & 5 deletions lib/svg-sprite/mode/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ SVGSpriteCss.prototype._init = function() {
}

// Refine the base data
this.data = _.assign(this.data, {
this.data = Object.assign(this.data, {
hasCommon : !!this.config.common,
common : this.config.common,
commonName : this.config.common || 'svg-common',
Expand Down Expand Up @@ -206,7 +206,7 @@ SVGSpriteCss.prototype._layout = function() {

// Remove all non-master shapes for non-displaceable sprites
if (!this._displaceable) {
this.data.shapes = _.reject(this.data.shapes, function(shape){ return !!shape.master; });
this.data.shapes = this.data.shapes.filter(function (shape) { return !shape.master; });
}

return {
Expand Down Expand Up @@ -377,7 +377,7 @@ SVGSpriteCss.prototype._addShapeToCSSSprite = function(shape, needsRegular, inde
}

// Register the SVG parameters
_.assign(this.data.shapes[index], {
Object.assign(this.data.shapes[index], {
first : !!(position & 1),
last : !!(position & 2),
position : {
Expand Down Expand Up @@ -419,7 +419,7 @@ SVGSpriteCss.prototype._addShapeToCSSSprite = function(shape, needsRegular, inde
* @return {File} SVG sprite file
*/
SVGSpriteCss.prototype._buildSVG = function(xmlDeclaration, doctypeDeclaration) {
var rootAttributes = _.extend(
var rootAttributes = Object.assign(
{},
this.config.svg.rootAttributes,
this.config.svg.dimensionAttributes ? {
Expand All @@ -431,7 +431,7 @@ SVGSpriteCss.prototype._buildSVG = function(xmlDeclaration, doctypeDeclaration)
}
),
svg = new SVGSprite(this.declaration(this.config.svg.xmlDeclaration, xmlDeclaration), this.declaration(this.config.svg.doctypeDeclaration, doctypeDeclaration), rootAttributes, true, this.config.svg.transform);
svg.add(_.map(this.data.shapes, 'svg'));
svg.add(Object.keys(this.data.shapes).map(key => this.data.shapes[key].svg));

return svg.toFile(this._spriter.config.dest, this._addCacheBusting(svg));
};
Expand Down
6 changes: 3 additions & 3 deletions lib/svg-sprite/mode/defs.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ SVGSpriteDefs.prototype.layout = function(files, cb) {
*/
SVGSpriteDefs.prototype._buildSVG = function(xmlDeclaration, doctypeDeclaration) {
var inline = !!this.config.inline,
rootAttributes = _.extend(
rootAttributes = Object.assign(
{},
this.config.svg.rootAttributes
),
svg = new SVGSprite(inline ? '' : this.declaration(this.config.svg.xmlDeclaration, xmlDeclaration), inline ? '' : this.declaration(this.config.svg.doctypeDeclaration, doctypeDeclaration), inline ? _.extend(
svg = new SVGSprite(inline ? '' : this.declaration(this.config.svg.xmlDeclaration, xmlDeclaration), inline ? '' : this.declaration(this.config.svg.doctypeDeclaration, doctypeDeclaration), inline ? Object.assign(
rootAttributes,
this.config.svg.dimensionAttributes ? {
width : 0,
Expand All @@ -85,7 +85,7 @@ SVGSpriteDefs.prototype._buildSVG = function(xmlDeclaration, doctypeDeclaration)
}
) : rootAttributes, !inline, this.config.svg.transform);
svg.add('<defs>');
svg.add(_.map(this.data.shapes, 'svg'));
svg.add(Object.keys(this.data.shapes).map(key => this.data.shapes[key].svg));
svg.add('</defs>');

return svg.toFile(this._spriter.config.dest, this._addCacheBusting(svg));
Expand Down
4 changes: 2 additions & 2 deletions lib/svg-sprite/mode/stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ SVGSpriteStack.prototype.layout = function(files, cb) {
* @return {File} SVG sprite file
*/
SVGSpriteStack.prototype._buildSVG = function(xmlDeclaration, doctypeDeclaration) {
var rootAttributes = _.extend(
var rootAttributes = Object.assign(
{},
this.config.svg.rootAttributes,
{
Expand All @@ -94,7 +94,7 @@ SVGSpriteStack.prototype._buildSVG = function(xmlDeclaration, doctypeDeclaration
),
svg = new SVGSprite(this.declaration(this.config.svg.xmlDeclaration, xmlDeclaration), this.declaration(this.config.svg.doctypeDeclaration, doctypeDeclaration), rootAttributes, true, this.config.svg.transform);
svg.add('<style>:root>svg{display:none}:root>svg:target{display:block}</style>');
svg.add(_.map(this.data.shapes, 'svg'));
svg.add(Object.keys(this.data.shapes).map(key => this.data.shapes[key].svg));

return svg.toFile(this._spriter.config.dest, this._addCacheBusting(svg));
};
Expand Down
4 changes: 2 additions & 2 deletions lib/svg-sprite/mode/standalone.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ SVGSpriteStandalone.prototype._layout = function(files, cb, extend) {
if (!shape.master) {
xmlDeclaration = xmlDeclaration || shape.xmlDeclaration;
doctypeDeclaration = doctypeDeclaration || shape.doctypeDeclaration;
_.assign(this.data.shapes[index], {
Object.assign(this.data.shapes[index], {
selector : {
dimensions : shape.state ? [{
expression : util.format(this.config.dimensions, shape.base) + ':' + shape.state,
Expand Down Expand Up @@ -101,7 +101,7 @@ SVGSpriteStandalone.prototype._layout = function(files, cb, extend) {
}, this);

// Remove all non-master shapes
this.data.shapes = _.reject(this.data.shapes, function(shape){ return !!shape.master; });
this.data.shapes = this.data.shapes.filter(function (shape) { return !shape.master; });

// Build the sprite SVG file
files.sprite = this._buildSVG(xmlDeclaration || '', doctypeDeclaration || '');
Expand Down
6 changes: 3 additions & 3 deletions lib/svg-sprite/mode/symbol.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ SVGSpriteSymbol.prototype.layout = function(files, cb) {
*/
SVGSpriteSymbol.prototype._buildSVG = function(xmlDeclaration, doctypeDeclaration) {
var inline = !!this.config.inline,
rootAttributes = _.extend(
rootAttributes = Object.assign(
{},
this.config.svg.rootAttributes
),
svg = new SVGSprite(inline ? '' : this.declaration(this.config.svg.xmlDeclaration, xmlDeclaration), inline ? '' : this.declaration(this.config.svg.doctypeDeclaration, doctypeDeclaration), inline ? _.extend(
svg = new SVGSprite(inline ? '' : this.declaration(this.config.svg.xmlDeclaration, xmlDeclaration), inline ? '' : this.declaration(this.config.svg.doctypeDeclaration, doctypeDeclaration), inline ? Object.assign(
rootAttributes,
this.config.svg.dimensionAttributes ? {
width : 0,
Expand All @@ -90,7 +90,7 @@ SVGSpriteSymbol.prototype._buildSVG = function(xmlDeclaration, doctypeDeclaratio
style : 'position:absolute'
}
) : rootAttributes, !inline, this.config.svg.transform);
svg.add(_.map(this.data.shapes, 'svg'));
svg.add(Object.keys(this.data.shapes).map(key => this.data.shapes[key].svg));

return svg.toFile(this._spriter.config.dest, this._addCacheBusting(svg));
};
Expand Down
2 changes: 1 addition & 1 deletion lib/svg-sprite/mode/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ SVGSpriteView.prototype._refineRootAttributes = function(shape, index, rootAttri
* @return {File} SVG sprite file
*/
SVGSpriteView.prototype._buildSVG = function(xmlDeclaration, doctypeDeclaration) {
var rootAttributes = _.extend(
var rootAttributes = Object.assign(
{},
this.config.svg.rootAttributes,
this.config.svg.dimensionAttributes ? {
Expand Down
4 changes: 2 additions & 2 deletions lib/svg-sprite/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ SVGSpriterQueue.prototype = Object.create(events.EventEmitter.prototype);
* @param {File} file Shape file
*/
SVGSpriterQueue.prototype.add = function(file) {
this._spriter.debug('Added "%s" to processing queue', file.path.substr(file.base.length + path.sep.length));
this._spriter.debug('Added "%s" to processing queue', path.basename(file.path));
this._files.push(file);
this.emit('add');
};
Expand All @@ -65,7 +65,7 @@ SVGSpriterQueue.prototype.process = function() {

// In case of errors: Skip the file
} catch(e) {
this._spriter.error('Skipping "%s" (%s)', file.path.substr(file.base.length + path.sep.length), e.message);
this._spriter.error('Skipping "%s" (%s)', path.basename(file.path), e.message);
this.emit(--this.active ? 'remove' : 'empty');
return;
}
Expand Down

0 comments on commit 475069f

Please sign in to comment.