Add ways to get substrings

This commit is contained in:
Evan Czaplicki 2013-10-06 15:51:08 -07:00
parent 74a13f59b5
commit 81cbe9f147
2 changed files with 51 additions and 6 deletions

View file

@ -62,6 +62,23 @@ Elm.Native.String.make = function(elm) {
}
return result;
}
function sub(start, end, str) {
return str.slice(start,end);
}
function left(n, str) {
return n < 1 ? "" : str.slice(0,n);
}
function right(n, str) {
return n < 1 ? "" : str.slice(-n);
}
function dropLeft(n, str) {
return n < 1 ? str : str.slice(n);
}
function dropRight(n, str) {
return n < 1 ? str : str.slice(0,-n);
}
function pad(n,chr,str) {
var half = n - str.length / 2;
return repeat(Math.ceil(half),chr) + str + repeat(half|0,chr);
@ -187,6 +204,12 @@ Elm.Native.String.make = function(elm) {
join: F2(join),
repeat: F2(repeat),
sub: F3(sub),
left: F2(left),
right: F2(right),
dropLeft: F2(dropLeft),
dropRight: F2(dropRight),
pad: F3(pad),
padLeft: F3(padLeft),
padRight: F3(padRight),

View file

@ -5,9 +5,12 @@ module String where
@docs map, filter, reverse, foldl, foldr
@docs split, join
@docs any, all, toUpper, toLower
@docs repeat
@docs split, join, repeat
Should the next 4 exist?
@docs left, right, dropLeft, dropRight, sub
@docs pad, padLeft, padRight
@ -15,10 +18,6 @@ module String where
@docs words, unwords, lines, unlines
@docs toUpper, toLower
@docs any, all
@docs contains, startsWith, endsWith, indices, indexes
@docs toInt, toFloat
@ -88,6 +87,29 @@ join = Native.String.join
repeat : Int -> String -> String
repeat = Native.String.repeat
sub : Int -> Int -> String -> String
sub = Native.String.sub
{-|
-}
left : Int -> String -> String
left = Native.String.left
{-|
-}
right : Int -> String -> String
right = Native.String.right
{-|
-}
dropLeft : Int -> String -> String
dropLeft = Native.String.dropLeft
{-|
-}
dropRight : Int -> String -> String
dropRight = Native.String.dropRight
{-|
-}
pad : Int -> Char -> String -> String