Last active
August 13, 2020 09:16
-
-
Save barelyhuman/842711daa2e060e8e9dba59fa65efb20 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {array} array The array you want length for | |
* @param {Object} options dictionary of strings for return | |
* @param {string} options.singular string for singular count | |
* @param {string} options.plural string for plural count | |
* @param {string} options.default string to return no matter the nature of the count | |
* @param {string} toInterpolate The string value wrapped in `{{}}` will be replaced by the count value on return | |
*/ | |
function getCount(array,options={},toInterpolate) { | |
const count = array.length; | |
options.default = options.default || "{{count}}"; | |
toInterpolate = toInterpolate || "count"; | |
if(count>0){ | |
if(options.plural){ | |
return stringInterpolate(options.plural,toInterpolate,count) | |
} | |
return stringInterpolate(options.default,toInterpolate,count) | |
} | |
else{ | |
if(options.singular){ | |
return stringInterpolate(options.singular,toInterpolate,count) | |
} | |
return stringInterpolate(options.default,toInterpolate,count); | |
} | |
} | |
/** | |
* @param {string} str The value to interpolate | |
* @param {string} toInterpolate The string value wrapped in `{{}}` will be replaced by the count value on return | |
* @param {string | number} value the value to replace the toInterpolate point with | |
*/ | |
function stringInterpolate(str,toInterpolate,value){ | |
const regex = new RegExp(`{{${toInterpolate}}}`,'g'); | |
return str.replace(regex,value); | |
} | |
// Example Run | |
const one = getCount([1,2],{ | |
singular:"Found {{user}} user", | |
plural:"Found {{user}} users", | |
},'user'); | |
const two = getCount([1],{ | |
default: "You've got {{user}}" | |
},'user'); | |
console.log({one,two}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation; | |
/** | |
* @param {array} array The array you want length for | |
* @param {Object} options dictionary of strings for return | |
* @param {string} options.singular string for singular count | |
* @param {string} options.plural string for plural count | |
* @param {string} options.default string to return no matter the nature of the count | |
* @param {string} toInterpolate The string value wrapped in `{{}}` will be replaced by the count value on return | |
*/ | |
struct Option{ | |
var singular="Found {{count}}"; | |
var plural="Found {{count}}"; | |
} | |
func getCount(array:Array<Any>,options:Option,toInterpolate:String)-> String { | |
let count = array.count; | |
var _toInterpolate="count"; | |
if(toInterpolate.count>0){ | |
_toInterpolate=toInterpolate | |
} | |
if(count>0){ | |
if(options.plural.count>0){ | |
return stringInterpolate(str:options.plural,toInterpolate:_toInterpolate,value:String(count)) | |
} | |
} | |
else{ | |
if(options.singular.count>0){ | |
return stringInterpolate(str:options.singular,toInterpolate:_toInterpolate,value:String(count)) | |
} | |
} | |
return String(count); | |
} | |
/** | |
* @param {string} str The value to interpolate | |
* @param {string} toInterpolate The string value wrapped in `{{}}` will be replaced by the count value on return | |
* @param {string | number} value the value to replace the toInterpolate point with | |
*/ | |
func stringInterpolate(str:String,toInterpolate:String,value: String)-> String { | |
let regex = ("{{"+toInterpolate+"}}"); | |
let result = str.replacingOccurrences(of: regex, with: value ); | |
return result; | |
} | |
// Example Run | |
var runOptions = Option(); | |
runOptions.singular = "Found {{soman}} user" | |
runOptions.plural = "Found {{soman}} users" | |
let result = getCount(array:[1],options:runOptions, | |
toInterpolate:"soman"); | |
let result2 = getCount(array:[1,2],options:runOptions,toInterpolate:"soman"); | |
print(result) | |
print(result2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment