/// <summary> /// 15.5.4.14 /// </summary> /// <param name="target"></param> /// <param name="parameters"></param> /// <returns></returns> public JsInstance SplitImpl(JsDictionaryObject target, JsInstance[] parameters) { JsObject A = Global.ArrayClass.New(); string S = target.Call(Global.Visitor, "toString").ToString(); if (parameters.Length == 0 || parameters[0] == JsUndefined.Instance) { A["0"] = Global.StringClass.New(S); } JsInstance separator = parameters[0]; int limit = parameters.Length > 1 ? Convert.ToInt32(parameters[1].ToNumber()) : Int32.MaxValue; int s = S.Length; string[] result; if (separator.Class == JsRegExp.TYPEOF) { var regexp = (Regex)((JsRegExp)separator).Value; result = regexp.Split(S, limit); } else { result = S.Split(new string[] { separator.ToString() }, limit, StringSplitOptions.None); } for (int i = 0; i < result.Length; i++) { A[i.ToString()] = Global.StringClass.New(result[i]); } return(A); }
/// <summary> /// 15.5.4.7 /// </summary> /// <param name="target"></param> /// <returns></returns> public JsInstance IndexOfImpl(JsDictionaryObject target, JsInstance[] parameters) { string source = target.Call(Global.Visitor, "toString").ToString(); string searchString = parameters[0].ToString(); int position = parameters.Length > 1 ? (int)parameters[1].ToNumber() : 0; if (searchString == String.Empty) { if (parameters.Length > 1) { return(Global.NumberClass.New(Math.Min(source.Length, position))); } else { return(Global.NumberClass.New(0)); } } if (position >= source.Length) { return(Global.NumberClass.New(-1)); } return(Global.NumberClass.New(source.IndexOf(searchString, position))); }
/// <summary> /// 15.5.4.8 /// </summary> /// <param name="target"></param> /// <returns></returns> public JsInstance LastIndexOfImpl(JsDictionaryObject target, JsInstance[] parameters) { string source = target.Call(Global.Visitor, "toString").ToString(); string searchString = parameters[0].ToString(); int position = parameters.Length > 1 ? (int)parameters[1].ToNumber() : source.Length; return(Global.NumberClass.New(source.Substring(0, position).LastIndexOf(searchString))); }
/// <summary> /// 15.5.4.6 /// </summary> /// <param name="target"></param> /// <returns></returns> public JsInstance ConcatImpl(JsDictionaryObject target, JsInstance[] parameters) { StringBuilder sb = new StringBuilder(); sb.Append(target.Call(Global.Visitor, "toString").ToString()); for (int i = 0; i < parameters.Length; i++) { sb.Append(parameters[i].Call(Global.Visitor, "toString").ToString()); } return(Global.StringClass.New(sb.ToString())); }
public JsInstance SubstrImpl(JsDictionaryObject target, JsInstance[] parameters) { string str = target.Call(Global.Visitor, "toString").ToString(); int start = 0, end = str.Length; if (parameters.Length > 0 && !double.IsNaN(parameters[0].ToNumber())) { start = Convert.ToInt32(parameters[0].ToNumber()); } if (parameters.Length > 1 && parameters[1] != JsUndefined.Instance && !double.IsNaN(parameters[1].ToNumber())) { end = Convert.ToInt32(parameters[1].ToNumber()); } start = Math.Min(Math.Max(start, 0), Math.Max(0, str.Length - 1)); end = Math.Min(Math.Max(end, 0), str.Length); str = str.Substring(start, end); return(New(str)); }
/// <summary> /// 15.5.5.1 /// </summary> /// <param name="target"></param> /// <returns></returns> public JsInstance LengthImpl(JsDictionaryObject target) { string str = target.Call(Global.Visitor, "toString").ToString(); return(Global.NumberClass.New(str.Length)); }
/// <summary> /// 15.5.4.19 /// </summary> /// <param name="target"></param> /// <param name="parameters"></param> /// <returns></returns> public JsInstance ToLocaleUpperCaseImpl(JsDictionaryObject target, JsInstance[] parameters) { string str = target.Call(Global.Visitor, "toString").ToString(); return(Global.StringClass.New(str.ToUpper())); }