on master help sign in
Add ways to get substrings
commit81cbe9f147cbd23fb63f97ef48ff84f7b87a73dd authorEvan Czaplicki <info@elm-lang.org> dateSun 6 Oct 2013, 15:51 (13 years ago) parent74a13f5
2 files changed+51−6
libraries/Native/String.js+23 libraries/String.elm+28−6
libraries/Native/String.js+23
@@ -62,6 +62,23 @@ Elm.Native.String.make = function(elm) {
6262 }
6363 return result;
6464 }
65+
66+ function sub(start, end, str) {
67+ return str.slice(start,end);
68+ }
69+ function left(n, str) {
70+ return n < 1 ? "" : str.slice(0,n);
71+ }
72+ function right(n, str) {
73+ return n < 1 ? "" : str.slice(-n);
74+ }
75+ function dropLeft(n, str) {
76+ return n < 1 ? str : str.slice(n);
77+ }
78+ function dropRight(n, str) {
79+ return n < 1 ? str : str.slice(0,-n);
80+ }
81+
6582 function pad(n,chr,str) {
6683 var half = n - str.length / 2;
6784 return repeat(Math.ceil(half),chr) + str + repeat(half|0,chr);
@@ -187,6 +204,12 @@ Elm.Native.String.make = function(elm) {
187204 join: F2(join),
188205 repeat: F2(repeat),
189206
207+ sub: F3(sub),
208+ left: F2(left),
209+ right: F2(right),
210+ dropLeft: F2(dropLeft),
211+ dropRight: F2(dropRight),
212+
190213 pad: F3(pad),
191214 padLeft: F3(padLeft),
192215 padRight: F3(padRight),
libraries/String.elm+28−6
@@ -5,9 +5,12 @@ module String where
55
66@docs map, filter, reverse, foldl, foldr
77
8−@docs split, join
8+@docs any, all, toUpper, toLower
99
10−@docs repeat
10+@docs split, join, repeat
11+
12+Should the next 4 exist?
13+@docs left, right, dropLeft, dropRight, sub
1114
1215@docs pad, padLeft, padRight
1316
@@ -15,10 +18,6 @@ module String where
1518
1619@docs words, unwords, lines, unlines
1720
18−@docs toUpper, toLower
19−
20−@docs any, all
21−
2221@docs contains, startsWith, endsWith, indices, indexes
2322
2423@docs toInt, toFloat
@@ -88,6 +87,29 @@ join = Native.String.join
8887repeat : Int -> String -> String
8988repeat = Native.String.repeat
9089
90+sub : Int -> Int -> String -> String
91+sub = Native.String.sub
92+
93+{-|
94+-}
95+left : Int -> String -> String
96+left = Native.String.left
97+
98+{-|
99+-}
100+right : Int -> String -> String
101+right = Native.String.right
102+
103+{-|
104+-}
105+dropLeft : Int -> String -> String
106+dropLeft = Native.String.dropLeft
107+
108+{-|
109+-}
110+dropRight : Int -> String -> String
111+dropRight = Native.String.dropRight
112+
91113{-|
92114-}
93115pad : Int -> Char -> String -> String