From 81cbe9f147cbd23fb63f97ef48ff84f7b87a73dd Mon Sep 17 00:00:00 2001 From: Evan Czaplicki Date: Sun, 6 Oct 2013 15:51:08 -0700 Subject: [PATCH] Add ways to get substrings --- libraries/Native/String.js | 23 +++++++++++++++++++++++ libraries/String.elm | 34 ++++++++++++++++++++++++++++------ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/libraries/Native/String.js b/libraries/Native/String.js index 31886de..3345fab 100644 --- a/libraries/Native/String.js +++ b/libraries/Native/String.js @@ -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), diff --git a/libraries/String.elm b/libraries/String.elm index ffc05fd..a46bef1 100644 --- a/libraries/String.elm +++ b/libraries/String.elm @@ -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