Version: 14
Array
uniqBy
Array.uniqBy(function)
Returns an array with unique elements. Does not affect the original array. Similar to frappe.utils.unique in Python's utiltiy functions.
let num_array = [10, 20, 10, 20, 30]
num_array.uniqBy(r => r) // [10, 20, 30]
move
Array.move(elementIndex, finalIndex)
Move an element in the array to a specified index. Pass the index of the element you want to move and the index of where you want to move to. Affects the original array.
let alphabet_array = ["a", "c", "b"]
alphabet_array.move(2, 1)
// ["a", "b", "c"]
Map
setDefault
Map.setDefault(key, defaultValue)
Pass a key to get back value. If there is no such key, create a new key-value pair with passed parameters as their respective keys and values. Similiar to Python's dict.setdefault.
let car = {
"brand": "Toyota"
}
car.setDefault("brand", "Mercendi") // "Toyota"
car.setDefault("age", 5) // 5
String
plural
String.plural()
Gives back the plural form of string. Works well for regular and irregular pluralization. Not working for uncountable words as it returns an list-like string with indices.
// Regular Words
"cow".plural() // "cows"
// Irregular Words
"index".plural() // "indices"
// Uncountable Words (Not working)
"sheep".plural() // String {"sheep"}
Functions
get_random
frappe.utils.get_random(length)
Returns a randomized string with specified length. If did not pass anything, returns a blank string. Characters included are letters in the English alphabets both lower and upper case.
frappe.utils.get_random(5)
// "ENeTw"
get_file_link
frappe.utils.get_file_link(filename)
Returns a relative path like files/fileName.fileExtension. As the filename is store like image-file-name.png, you will have to pass both filename and extension as a single string. If not, the url generated will be incorrect.
frappe.utils.get_file_link("dev-navbar-logo.png")
// "files/dev-navbar-logo.png"
replace_newlines
frappe.utils.replace_newlines(string)
Replaces "\n" to <br> in a string.
frappe.utils.replace_newlines("David
NewLines")
// "David<br>NewLines"
is_html
frappe.utils.is_html(string)
Searchs for <br> , <p, <img, <div, and <span tags. Returns true if mentioned tags are found. Returns false if no string or did not find mentioned tags.
frappe.utils.is_html("<div>Hello</div>")
// true
frappe.utils.is_html("<section>Hello</section>")
// false
is_mac
frappe.utils.is_mac()
Searchs for platform with window.navigator.platform. Returns true if platform is equal to MacIntel. Returns false if not.
"On Other OS"
frappe.utils.is_mac()
// false
"On Mac"
frappe.utils.is_mac()
// true
frappe.datetime.add_days
frappe.datetime.add_days('2019-09-08', 1)
frappe.datetime.add_months
frappe.datetime.add_months('2019-09-08', 1)
frappe.format (Currency)
frappe.format(10000, { fieldtype: 'Currency' })
Output => 10,000