Skip to content

Commit

Permalink
chore(release): 2.0.1 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Aug 27, 2018
1 parent 771c43a commit d57cb1c
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 52 deletions.
9 changes: 6 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
interface Options {
context: string,
hashPrefix: string,
context: string;
hashPrefix: string;
}

type Generator = (localName: string, filepath: string) => string;

declare function createGenerator(pattern: string, options?: Partial<Options>): Generator;
declare function createGenerator(
pattern: string,
options?: Partial<Options>
): Generator;

export = createGenerator;
30 changes: 15 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';
"use strict";

var interpolateName = require('loader-utils').interpolateName;
var path = require('path');
var util = require('util');
var interpolateName = require("loader-utils").interpolateName;
var path = require("path");

/**
* @param {string} pattern
Expand All @@ -13,12 +12,12 @@ var util = require('util');
*/
module.exports = function createGenerator(pattern, options) {
options = options || {};
var context = options && typeof options.context === 'string'
? options.context
: process.cwd();
var hashPrefix = options && typeof options.hashPrefix === 'string'
? options.hashPrefix
: '';
var context =
options && typeof options.context === "string"
? options.context
: process.cwd();
var hashPrefix =
options && typeof options.hashPrefix === "string" ? options.hashPrefix : "";

/**
* @param {string} localName Usually a class name
Expand All @@ -32,16 +31,17 @@ module.exports = function createGenerator(pattern, options) {
};

var loaderOptions = {
content: util.format('%s%s+%s',
hashPrefix,
path.relative(context, filepath),
localName),
content:
hashPrefix +
path.relative(context, filepath).replace(/\\/g, "/") +
"+" +
localName,
context: context
};

var genericName = interpolateName(loaderContext, name, loaderOptions);
return genericName
.replace(new RegExp('[^a-zA-Z0-9\\-_\u00A0-\uFFFF]', 'g'), '-')
.replace(new RegExp("[^a-zA-Z0-9\\-_\u00A0-\uFFFF]", "g"), "-")
.replace(/^((-?[0-9])|--)/, "_$1");
};
};
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "generic-names",
"version": "2.0.0",
"version": "2.0.1",
"description": "Helper for building generic names, similar to webpack",
"main": "index.js",
"types": "index.d.ts",
Expand Down
50 changes: 31 additions & 19 deletions test/cssspec.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,58 @@
'use strict';
"use strict";

const genericNames = require('../index');
const test = require('tape');
const path = require('path');
const genericNames = require("../index");
const test = require("tape");
const path = require("path");

// According to the CSS spec, identifiers cannot
// start with a digit, two hyphens, or a hyphen
// followed by a digit.
//
// relates: https://github.com/webpack/css-loader/commit/da27c07d0df25a38699344c13b6614c53a469fd9

test('identity', t => {
const generate = genericNames('[local]');
test("identity", t => {
const generate = genericNames("[local]");

t.equal(generate('foo', path.join(__dirname, 'test/case/source.css')), 'foo');
t.equal(generate("foo", path.join(__dirname, "test/case/source.css")), "foo");
t.end();
});

test('leading digit', t => {
const generate = genericNames('0[local]');
test("leading digit", t => {
const generate = genericNames("0[local]");

t.equal(generate('foo', path.join(__dirname, 'test/case/source.css')), '_0foo');
t.equal(
generate("foo", path.join(__dirname, "test/case/source.css")),
"_0foo"
);
t.end();
});

test('leading digit in the token', t => {
const generate = genericNames('[local]');
test("leading digit in the token", t => {
const generate = genericNames("[local]");

t.equal(generate('0foo', path.join(__dirname, 'test/case/source.css')), '_0foo');
t.equal(
generate("0foo", path.join(__dirname, "test/case/source.css")),
"_0foo"
);
t.end();
});

test('leading two hyphens', t => {
const generate = genericNames('--[local]');
test("leading two hyphens", t => {
const generate = genericNames("--[local]");

t.equal(generate('foo', path.join(__dirname, 'test/case/source.css')), '_--foo');
t.equal(
generate("foo", path.join(__dirname, "test/case/source.css")),
"_--foo"
);
t.end();
});

test('leading hyphen and digit', t => {
const generate = genericNames('-0[local]');
test("leading hyphen and digit", t => {
const generate = genericNames("-0[local]");

t.equal(generate('foo', path.join(__dirname, 'test/case/source.css')), '_-0foo');
t.equal(
generate("foo", path.join(__dirname, "test/case/source.css")),
"_-0foo"
);
t.end();
});
40 changes: 27 additions & 13 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
'use strict';
"use strict";

const genericNames = require('../index');
const test = require('tape');
const path = require('path');
const genericNames = require("../index");
const test = require("tape");
const path = require("path");

const pattern = '[name]__[local]___[hash:base64:5]';
const pattern = "[name]__[local]___[hash:base64:5]";

test('use `cwd` if no context was provided', t => {
test("use `cwd` if no context was provided", t => {
const generate = genericNames(pattern);

t.equal(generate('foo', path.join(__dirname, 'test/case/source.css')), 'source__foo___3D34a');
t.equal(
generate("foo", path.join(__dirname, "test/case/source.css")),
"source__foo___3D34a"
);
t.end();
});

test('generate distinct hash for the provided context', t => {
const generate = genericNames(pattern, {context: path.join(__dirname, '/test') });
test("generate distinct hash for the provided context", t => {
const generate = genericNames(pattern, {
context: path.join(__dirname, "/test")
});

t.equal(generate('foo', path.join(__dirname, 'test/case/source.css')), 'source__foo___19xFw');
t.equal(
generate("foo", path.join(__dirname, "test/case/source.css")),
"source__foo___19xFw"
);
t.end();
});

test('generate distinct hash for the provided hashPrefix', t => {
const generate = genericNames(pattern, {context: path.join(__dirname, '/test'), hashPrefix: '--'});
test("generate distinct hash for the provided hashPrefix", t => {
const generate = genericNames(pattern, {
context: path.join(__dirname, "/test"),
hashPrefix: "--"
});

t.equal(generate('foo', path.join(__dirname, 'test/case/source.css')), 'source__foo___3T0Un');
t.equal(
generate("foo", path.join(__dirname, "test/case/source.css")),
"source__foo___3T0Un"
);
t.end();
});

0 comments on commit d57cb1c

Please sign in to comment.