Skip to content

Commit

Permalink
fix(inflector): fix and improve support for inflector
Browse files Browse the repository at this point in the history
Added test to inflector, fixs issues while inflecting, added more options to inflections
  • Loading branch information
gerard2perez committed Nov 12, 2016
1 parent 38d31f5 commit 78be90c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/support/inflector.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
import * as i from 'i';

const inflector = i();
const inflections = requireSafe(ProyPath('config', 'inflections'), {
const inflections = Object.assign({}, {
plural: [],
irregular: [],
singular: [],
uncontable: []
uncountable: []
}, requireSafe(ProyPath('config', 'inflections'), {}));

let uncountable = inflections.uncountable.map((inflection) => {
return [inflection, `/${inflection}/`]
});

inflections.singular.forEach((inflect) => {
inflector.inflections.singular(inflect[1], inflect[0]);
});

let irregular = (inflections.plural || [])
.concat(inflections.singular || [])
.concat(inflections.irregular || []),
uncontable = (inflections.uncountable || []).map((inflection) => {
return `/${inflection}/`
});
irregular.forEach((inflect) => {
inflections.plural.forEach((inflect) => {
inflector.inflections.irregular(inflect[0], inflect[1]);
});
uncontable.forEach((inflect) => {

inflections.irregular.forEach((inflect) => {
inflector.inflections.irregular(inflect[0], inflect[1]);
});

uncountable.forEach((inflect) => {
console.log(inflect)
inflector.inflections.uncountable(inflect[0]);
});

export default inflector;
38 changes: 38 additions & 0 deletions test/unit/support/inflector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*global describe, it*/
import * as assert from 'assert';
import * as fs from 'fs-extra';

let inflectorFile = `
module.exports={
plural:[['person','people']],
singular:[['mice','mouse']],
irregular:[['chilaquil','chilaquiles']],
uncountable:['oil']
}
`;

fs.outputFileSync(ProyPath('config', 'inflections.js'), inflectorFile);
const inflector = require('../../../src/support/inflector').default;

fs.removeSync(ProyPath('config'));

describe('Inflector', function() {
it('person <-> people', function() {
assert.equal("person", inflector.singularize('people'), 'singular');
assert.equal("people", inflector.pluralize('person'), 'plural');
});

it('mice <-> mouse', function() {
assert.equal("mouse", inflector.singularize('mice'), 'singular');
assert.equal("mice", inflector.pluralize('mouse'), 'plural');
});
it('chilaquil <-> chilaquiles', function() {
assert.equal("chilaquil", inflector.singularize('chilaquiles'), 'singular');
assert.equal("chilaquiles", inflector.pluralize('chilaquil'), 'plural');
});
it('oil <-> oil', function() {
assert.equal("oil", inflector.singularize('oil'), 'singular');
assert.equal("oil", inflector.pluralize('oil'), 'plural');
});

});

0 comments on commit 78be90c

Please sign in to comment.