Skip to content
This repository has been archived by the owner on Jul 6, 2018. It is now read-only.

Add typings for ember-inflector #23

Merged
merged 3 commits into from
Sep 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions types/ember-inflector/ember-inflector-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import Ember from 'ember';
import Inflector, { singularize, pluralize } from 'ember-inflector';

Inflector.inflector.singularize("tacos"); // taco
Inflector.inflector.pluralize("taco"); // tacos

singularize("tacos"); // taco
pluralize("taco"); // tacos

Ember.String.singularize("tacos"); // taco
Ember.String.pluralize("taco"); // tacos

// or if not using Ember CLI/ES6
Ember.Inflector.inflector.pluralize("taco"); // tacos

const inflector = Inflector.inflector;
const inflector2 = new Inflector();
const inflector3 = new Ember.Inflector();
const inflector4 = new Ember.Inflector(Ember.Inflector.defaultRules);

const customRules = {
plurals: [
[ /$/, 's' ]
],
singular: [
[ /\s$/, '' ]
],
irregularPairs: [
[ 'cow', 'kine' ]
],
uncountable: [ 'fish' ]
};

const inflector5 = new Ember.Inflector(customRules);

inflector.irregular('formula', 'formulae');
inflector.uncountable('advice');

inflector.pluralize('cow'); // => 'kine'
inflector.singularize('kine'); // => 'cow'

inflector.pluralize('advice'); // => 'advices'
inflector.uncountable('advice');
inflector.pluralize('advice'); // => 'advice'

inflector.pluralize('formula'); // => 'formulas'
inflector.irregular('formula', 'formulae');
inflector.pluralize('formula'); // => 'formulae'

// you would not need to add these as they are the default rules
inflector.plural(/$/, 's');
inflector.singular(/s$/i, '');
80 changes: 80 additions & 0 deletions types/ember-inflector/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Type definitions for ember-inflector 2.0
// Project: https://github.com/emberjs/ember-inflector#readme
// Definitions by: Derek Wickern <https://github.com/dwickern>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.4

declare module 'ember-inflector' {
type Mapping = Array<string | RegExp>;

interface RuleSet {
plurals?: Mapping[];
singular?: Mapping[];
irregularPairs?: Mapping[];
uncountable?: string[];
}

/**
* Inflector.Ember provides a mechanism for supplying inflection rules for your
* application. Ember includes a default set of inflection rules, and provides an
* API for providing additional rules.
*/
class Inflector {
static inflector: Inflector;
static defaultRules: RuleSet;

constructor(ruleSet?: RuleSet);

enableCache(): void;
purgeCache(): void;
disableCache(): void;
plural(regex: RegExp, string: string): void;
singular(regex: RegExp, string: string): void;
uncountable(regex: string): void;
irregular(singular: string, plural: string): void;
pluralize(word: string): string;
singularize(word: string): string;
protected inflect(word: string, typeRules: {}, irregular: {}): string;
}

export function pluralize(word: string): string;
export function singularize(word: string): string;
export const defaultRules: RuleSet;
export default Inflector;
}

declare module 'ember' {
import EmberInflector from 'ember-inflector';

namespace Ember {
export const Inflector: typeof EmberInflector;

namespace String {
export function pluralize(word: string): string;
export function singularize(word: string): string;
}

namespace HTMLBars {
class helpers {
/**
* If you have Ember Inflector (such as if Ember Data is present),
* pluralize a word. For example, turn "ox" into "oxen".
* Example:
* {{pluralize count myProperty}}
* {{pluralize 1 "oxen"}}
* {{pluralize myProperty}}
* {{pluralize "ox"}}
*/
pluralize(count: number, word: string): any;
/**
* If you have Ember Inflector (such as if Ember Data is present),
* singularize a word. For example, turn "oxen" into "ox".
* Example:
* {{singularize myProperty}}
* {{singularize "oxen"}}
*/
singularize(word: string): any;
}
}
}
}
18 changes: 18 additions & 0 deletions types/ember-inflector/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"ember-inflector-tests.ts"
]
}
6 changes: 6 additions & 0 deletions types/ember-inflector/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "dtslint/dt.json" ,
"rules": {
"strict-export-declare-modifiers": false
}
}
2 changes: 1 addition & 1 deletion types/ember/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ interface RenderOptions {
view?: string;
}

namespace Ember {
export namespace Ember {
/**
Alias for jQuery.
**/
Expand Down