Comparing version 1.0.0 to 1.1.0
@@ -17,6 +17,6 @@ 'use strict'; | ||
Inflections.getInstance = function(locale) { | ||
process.__Rejoin_Inflections = process.__Rejoin_Inflections || {}; | ||
process.__Rejoin_Inflections[locale] = process.__Rejoin_Inflections[locale] || new Inflections(); | ||
process.__Inflector_Inflections = process.__Inflector_Inflections || {}; | ||
process.__Inflector_Inflections[locale] = process.__Inflector_Inflections[locale] || new Inflections(); | ||
return process.__Rejoin_Inflections[locale]; | ||
return process.__Inflector_Inflections[locale]; | ||
}; | ||
@@ -23,0 +23,0 @@ |
'use strict'; | ||
var Inflections = require('./Inflections'); | ||
var Methods = require('./Methods'); | ||
var defaults = require('./defaults'); | ||
var isFunc = require('./isFunc'); | ||
var Inflections = require('./Inflections'); | ||
var Transliterator = require('./Transliterator'); | ||
var Methods = require('./Methods'); | ||
var defaults = require('./defaults'); | ||
var isFunc = require('./isFunc'); | ||
@@ -25,2 +26,17 @@ var Inflector = Methods; | ||
Inflector.transliterations = function(locale, fn) { | ||
if (isFunc(locale)) { | ||
fn = locale; | ||
locale = null; | ||
} | ||
locale = locale || 'en'; | ||
if (fn) { | ||
fn(Transliterator.getInstance(locale)); | ||
} else { | ||
return Transliterator.getInstance(locale); | ||
} | ||
} | ||
for (var locale in defaults) { | ||
@@ -27,0 +43,0 @@ Inflector.inflections(locale, defaults[locale]); |
@@ -145,2 +145,40 @@ 'use strict'; | ||
transliterate: function(string, options) { | ||
options = options || {}; | ||
var locale = options.locale || 'en'; | ||
var replacement = options.replacement || '?'; | ||
return this.transliterations(locale).transliterate(string, replacement); | ||
}, | ||
parameterize: function(string, options) { | ||
options = options || {}; | ||
if (options.separator === undefined) { | ||
options.separator = '-'; | ||
} | ||
if (options.separator === null) { | ||
options.separator = ''; | ||
} | ||
// replace accented chars with their ascii equivalents | ||
var result = this.transliterate(string, options); | ||
result = result.replace(/[^a-z0-9\-_]+/ig, options.separator); | ||
if (options.separator.length) { | ||
var separatorRegex = new RegExp(options.separator); | ||
// no more than one of the separator in a row | ||
result = result.replace(new RegExp(separatorRegex.source + '{2,}'), options.separator); | ||
// remove leading/trailing separator | ||
result = result.replace(new RegExp('^' + separatorRegex.source + '|' + separatorRegex.source + '$', 'i'), ''); | ||
} | ||
return result.toLowerCase(); | ||
}, | ||
_applyInflections: function(word, rules) { | ||
@@ -147,0 +185,0 @@ var result = '' + word, rule, regex, replacement; |
{ | ||
"name": "inflected", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "A port of ActiveSupport's inflector to Node.js", | ||
@@ -38,8 +38,11 @@ "main": "index.js", | ||
"dependencies": {}, | ||
"peerDependencies": {}, | ||
"peerDependencies": { | ||
"counterpart": ">=0.11" | ||
}, | ||
"devDependencies": { | ||
"jshint": "^2.4.4", | ||
"mocha": "^1.17.1", | ||
"extend": "^1.2.1" | ||
"extend": "^1.2.1", | ||
"counterpart": ">=0.11" | ||
} | ||
} |
87
spec.js
@@ -10,6 +10,12 @@ var assert = require('assert'); | ||
function withDup(fn) { | ||
var original = process.__Rejoin_Inflections; | ||
process.__Rejoin_Inflections = extend(true, {}, original); | ||
var originalInflections = process.__Inflector_Inflections; | ||
process.__Inflector_Inflections = extend(true, {}, originalInflections); | ||
var originalTransliterator = process.__Inflector_Transliterator; | ||
process.__Inflector_Transliterator = null; | ||
fn(); | ||
process.__Rejoin_Inflections = original; | ||
process.__Inflector_Transliterator = originalTransliterator; | ||
process.__Inflector_Inflections = originalInflections; | ||
} | ||
@@ -25,3 +31,3 @@ | ||
describe('Rejoin.Inflector', function() { | ||
describe('Inflector', function() { | ||
it('properly pluralizes plurals', function() { | ||
@@ -229,3 +235,3 @@ assert.equal(Inflector.pluralize('plurals'), 'plurals'); | ||
assert.equal(Inflector.classify(tableName), className); | ||
assert.equal(Inflector.classify("table_prefix." + tableName), className); | ||
assert.equal(Inflector.classify('table_prefix.' + tableName), className); | ||
}); | ||
@@ -254,11 +260,11 @@ }); | ||
assert.equal(Inflector.humanize("jargon_cnt"), "Jargon count"); | ||
assert.equal(Inflector.humanize("prefx_request"), "Request"); | ||
assert.equal(Inflector.humanize('jargon_cnt'), 'Jargon count'); | ||
assert.equal(Inflector.humanize('prefx_request'), 'Request'); | ||
}); | ||
it('properly humanizes by string', function() { | ||
inflect.human("col_rpted_bugs", "Reported bugs"); | ||
inflect.human('col_rpted_bugs', 'Reported bugs'); | ||
assert.equal(Inflector.humanize("col_rpted_bugs"), "Reported bugs"); | ||
assert.equal(Inflector.humanize("COL_rpted_bugs"), "Col rpted bugs"); | ||
assert.equal(Inflector.humanize('col_rpted_bugs'), 'Reported bugs'); | ||
assert.equal(Inflector.humanize('COL_rpted_bugs'), 'Col rpted bugs'); | ||
}); | ||
@@ -377,3 +383,3 @@ | ||
inflect.uncountable('series'); | ||
inflect.human("col_rpted_bugs", "Reported bugs"); | ||
inflect.human('col_rpted_bugs', 'Reported bugs'); | ||
@@ -397,3 +403,3 @@ inflect.clear('all'); | ||
inflect.uncountable('series'); | ||
inflect.human("col_rpted_bugs", "Reported bugs"); | ||
inflect.human('col_rpted_bugs', 'Reported bugs'); | ||
@@ -409,2 +415,59 @@ inflect.clear(); | ||
}); | ||
it('properly parameterizes', function() { | ||
objEach(TestCases.StringToParameterized, function(someString, parameterizedString) { | ||
assert.equal(Inflector.parameterize(someString), parameterizedString); | ||
}); | ||
}); | ||
it('properly parameterizes and normalizes', function() { | ||
objEach(TestCases.StringToParameterizedAndNormalized, function(someString, parameterizedString) { | ||
assert.equal(Inflector.parameterize(someString), parameterizedString); | ||
}); | ||
}); | ||
it('properly parameterizes with custom separator', function() { | ||
objEach(TestCases.StringToParameterizeWithUnderscore, function(someString, parameterizedString) { | ||
assert.equal(Inflector.parameterize(someString, { separator: '_' }), parameterizedString); | ||
}); | ||
}); | ||
it('properly parameterizes with no separator', function() { | ||
objEach(TestCases.StringToParameterizeWithNoSeparator, function(someString, parameterizedString) { | ||
assert.equal(Inflector.parameterize(someString, { separator: null }), parameterizedString); | ||
assert.equal(Inflector.parameterize(someString, { separator: '' }), parameterizedString); | ||
}); | ||
}); | ||
it('properly parameterizes with multi character separator', function() { | ||
objEach(TestCases.StringToParameterized, function(someString, parameterizedString) { | ||
assert.equal(Inflector.parameterize(someString, { separator: '__sep__' }), parameterizedString.replace(/-/g, '__sep__')); | ||
}); | ||
}); | ||
it('allows overwriting transliterate approximations', function() { | ||
withDup(function() { | ||
assert.equal(Inflector.parameterize('Jürgen'), 'jurgen'); | ||
Inflector.transliterations(function(transliterate) { | ||
transliterate.approximate('ü', 'ue'); | ||
}); | ||
assert.equal(Inflector.parameterize('Jürgen'), 'juergen'); | ||
}); | ||
}); | ||
it('allows overwriting transliterate approximations for a specific locale', function() { | ||
withDup(function() { | ||
assert.equal(Inflector.parameterize('Jürgen'), 'jurgen'); | ||
assert.equal(Inflector.parameterize('Jürgen', { locale: 'de' }), 'jurgen'); | ||
Inflector.transliterations('de', function(transliterate) { | ||
transliterate.approximate('ü', 'ue'); | ||
}); | ||
assert.equal(Inflector.parameterize('Jürgen'), 'jurgen'); | ||
assert.equal(Inflector.parameterize('Jürgen', { locale: 'de' }), 'juergen'); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46220
18
1072
1
4