Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for "count" & "returnCount" parameters to avoid having to use IF/ELSE #5

Open
JamoCA opened this issue Jul 1, 2020 · 0 comments

Comments

@JamoCA
Copy link

JamoCA commented Jul 1, 2020

When using pluralise or singularise, developers are required to add if/else logic to determine which function to use. I borrowed some logic from CFWeels which has a Pluralize() function that accepts two additional parameters: count and returnCount. I've added these to the inflector CFC that we use and am sharing it in case anyone else is interested.

<cffunction name="pluralise" access="public" returntype="string" output="no" hint="Returns the plural form of a word">
	<cfargument name="word" type="string" required="yes" hint="The word to get the plural for">
	<cfargument name="count" type="numeric" required="no" default="-1" hint="Pluralization will occur when this value is not 1.">
	<cfargument name="returnCount" type="boolean" required="no" default="false" hint="Will return count prepended to the pluralization when true and count is not -1.">
	<cfset var i = 0>
	<cfif arguments.count NEQ -1 and arguments.count EQ 1>
		<cfset arguments.word = singularise(arguments.word)>
	<cfelse>
		<cfif NOT listfind(variables.uncountables, lcase(arguments.word))>
			<cfloop from="1" to="#arraylen(variables.plurals)#" index="i">
				<cfif refindnocase(variables.plurals[i]['rule'], arguments.word)>
					<cfset arguments.word = rereplacenocase(arguments.word, variables.plurals[i]['rule'], variables.plurals[i]['replacement'])>
					<cfbreak>
				</cfif>
			</cfloop>
		</cfif>
	</cfif>
	<cfif arguments.count NEQ -1 and arguments.returnCount>
		<cfset arguments.word = NumberFormat(arguments.count) & " " & arguments.word>
	</cfif>
	<cfreturn arguments.word>
</cffunction>

I've also added support for calling it as pluralize (with a z) since that's the way it's spelled in our region.

<cfset pluralize = pluralise>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant