String case: Difference between revisions

→‎{{header|AppleScript}}: Updated primitives
No edit summary
(→‎{{header|AppleScript}}: Updated primitives)
Line 329:
=={{header|AppleScript}}==
{{Trans|JavaScript}}
 
 
AppleScript lacks built in string case functions, but since OS X 10.10 (Yosemite version, Oct 2014) it has been possible to use ObjC Foundation class methods directly in AppleScript code.
 
<lang applescript>use framework "Foundation"
 
-- toUpperCase :: Text -> Text
on toUpperCase(str)
set ca to current application
((ca's NSString's stringWithString:(str))'s ¬
uppercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toUpperCase
 
-- toLowerCase :: Text -> Text
on toLowerCase(str)
set ca to current application
((ca's NSString's stringWithString:(str))'s ¬
lowercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toLowerCase
 
-- toCapitalized :: Text -> Text
on toCapitalized(str)
set ca to current application
((ca's NSString's stringWithString:(str))'s ¬
capitalizedStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toCapitalized
 
 
-- TEST -----------------------------------------------------------------------
-- TEST
on run
-- testCase :: Handler -> String
script testCase
on lambda|λ|(f)
mReturn(f)'s lambda|λ|("alphaBETA αβγδΕΖΗΘ")
end lambda|λ|
end script
map(testCase, {toUpperCasetoUpper, toLowerCasetoLower, toCapitalizedtoTitle})
end run
 
 
-- GENERIC FUNCTIONS ----------------------------------------------------------
 
-- toLower :: String -> String
on toUpperCasetoLower(str)
set ca to current application
((ca's NSString's stringWithString:(str))'s ¬
lowercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toLower
 
-- toTitle :: String -> String
on toLowerCasetoTitle(str)
set ca to current application
((ca's NSString's stringWithString:(str))'s ¬
capitalizedStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toTitle
 
-- toUpper :: String -> String
on toUpper(str)
set ca to current application
((ca's NSString's stringWithString:(str))'s ¬
uppercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toUpper
 
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
Line 379:
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda|λ|(item i of xs, i, xs)
end repeat
return lst
Line 392:
else
script
property lambda|λ| : f
end script
end if
end mReturn</lang>
 
{{Out}}
<lang AppleScript>{"ALPHABETA ΑΒΓΔΕΖΗΘ", "alphabeta αβγδεζηθ", "Alphabeta Αβγδεζηθ"}</lang>
9,659

edits