const { hasProperty } = require('../utils');

module.exports = function contains(schema, tpl) {
  if (!hasProperty(schema, 'contains')) {
    return;
  }

  const error = tpl.error('contains');
  const fn = `${tpl.link(schema.contains)}`;
  const condition = `${fn}(%s)`;

  tpl('if (Array.isArray(%s)) {', tpl.data);
  tpl(`if (%s.length === 0) ${error}`, tpl.data);
  tpl('for ($1; $2 < $3.length; $2++) {', tpl.cache('0'), tpl.cache('0'), tpl.data);
  tpl.data.push(`[${tpl.cache('0')}]`);
  tpl(`if (!${condition}) break;`, tpl.data);
  tpl.data.pop();
  tpl(`if ($1 === $2.length - 1) ${error}`, tpl.cache('0'), tpl.data);
  tpl('}');
  tpl('}');
};

@@ -232,24 +232,25 @@
 ins)%7D%60;%0A
+%0A
   const 
 conditio
@@ -245,31 +245,160 @@
 nst 
-condition = %60$%7Bfn%7D(%25s)%60
+%7B data %7D = tpl;%0A  const zeroIndex = tpl.cache('0');%0A  const index = tpl.cache('0');%0A  const dataAtIndex = data.toString.apply(data.concat(%60%5B$%7Bindex%7D%5D%60))
 ;%0A%0A 
@@ -402,17 +402,17 @@
 %0A%0A  tpl(
-'
+%60
 if (Arra
@@ -425,359 +425,234 @@
 ray(
-%25s)) %7B', tpl.data);%0A  tpl(%60if (%25s.length === 0) $%7Berror%7D%60, tpl.data);%0A  tpl('for ($1; $2 %3C $3.length; $2++) %7B', tpl.cache('0'), tpl.cache('0'), tpl.data);%0A  tpl.data.push(%60%5B$%7Btpl.cache('0')%7D%5D%60);%0A  tpl(%60if (!$%7Bcondition%7D) break;%60, tpl.data);%0A  tpl.data.pop();%0A  tpl(%60if ($1 === $2.length - 1) $%7Berror%7D%60, tpl.cache('0'), tpl.data);%0A  tpl('%7D');%0A  tpl('%7D'
+$%7Bdata%7D)) %7B%0A    if ($%7Bdata%7D.length === 0) $%7Berror%7D%0A      for ($%7BzeroIndex%7D; $%7Bindex%7D %3C $%7Bdata%7D.length; $%7Bindex%7D++) %7B%0A        if (!$%7Bfn%7D($%7BdataAtIndex%7D)) break;%0A        if ($%7Bindex%7D === $%7Bdata%7D.length - 1) $%7Berror%7D%0A      %7D%0A  %7D%60
 );%0A%7D

Change contains template function