import _ from "lodash/core";
import capitalize from "lodash/capitalize";
import repeat from "lodash/repeat";

export default {
  // Returns the char at the position of the given utf8 string.
  // If position is greater than string length, then it returns nil.
  // at(t, integer) :: string | null
  at(str, position) {
    const isNegative = Math.sign(position) === -1;

    if (position >= str.length) {
      return null;
    }
    else if (isNegative) {
      return str.charAt(str.length - Math.abs(position)) || null;
    }
    return str.charAt(position);
  },

  // Converts the first character in the given string to
  // uppercase and the remainder to lowercase.
  // capitalize(string) :: string
  capitalize,

  // Checks if string contains any of the given contents.
  // contents can be either a single string or a list of strings.
  // contains(string, [string] | string) :: boolean
  contains(str, pat) {
    let patterns;
    if (_.isArray(pat)) {
      patterns = pat;
    }
    else {
      patterns = [pat];
    }

    const results = patterns.map(pattern => {
      const result = str.match(pattern);
      return result !== null;
    });

    return results.indexOf(true) >= 0;
  },

  // Converts all characters in the given string to lowercase.
  // downcase(string) :: string
  downcase(str) {
    return str.toLowerCase();
  },

  // Returns a string subject duplicated n times.
  // duplicate(string, non_neg_integer) :: string
  duplicate: repeat,
};

@@ -1,12 +1,21 @@
+// @flow%0A
 import _ fro
@@ -307,16 +307,24 @@
   at(str
+: string
 , positi
@@ -325,17 +325,34 @@
 position
-)
+: number): ?string
  %7B%0A    c
@@ -942,171 +942,89 @@
 (str
-, pat) %7B%0A    let patterns;%0A    if (_.isArray(pat)) %7B%0A      patterns = pat;%0A    %7D%0A    else %7B%0A      patterns = %5Bpat%5D;%0A    %7D%0A%0A    const results = patterns.map
+: string, patterns: Array%3Cstring%3E %7C string): boolean %7B%0A    if (_.isString
 (pattern
  =%3E 
@@ -1023,11 +1023,11 @@
 tern
- =%3E
+s))
  %7B%0A 
@@ -1035,120 +1035,104 @@
     
-const result = str.match(
+return do_contains(str, %5B
 pattern
+s%5D
 );%0A    
-  return result !== null;%0A    %7D);%0A%0A
+%7D else %7B%0A   
    
- 
 return 
-results.indexOf(true) %3E= 0;
+do_contains(str, patterns);%0A    %7D
 %0A  %7D
@@ -1243,17 +1243,33 @@
 case(str
-)
+: string): string
  %7B%0A    r
@@ -1423,8 +1423,236 @@
 eat,%0A%7D;%0A
+%0Afunction do_contains(str: string, patterns: Array%3Cstring%3E): boolean %7B%0A  const results = patterns.map(pattern =%3E %7B%0A    const result = str.match(pattern);%0A    return result !== null;%0A  %7D);%0A%0A  return results.indexOf(true) %3E= 0;%0A%7D%0A

Implement flow for string module