/// <summary> /// Joins two or more arrays, and returns a copy of the joined arrays /// </summary> /// <param name="target">The target list to apply this method on.</param> /// <param name="arrays">Array of arrays to add</param> /// <returns>A copy of joined array</returns> public LObject Concat(LObject target, params object[] arrays) { if (arrays == null || arrays.Length == 0) return target; var list = target.GetValue() as IList; var copy = new List<object>(); AddRange(copy, list); for (var ndx = 0; ndx < arrays.Length; ndx++) { object item = arrays[ndx]; IList array = (IList)item; AddRange(copy, array); } return new LArray(copy); }
/// <summary> /// Adds new elements to the beginning of an array, and returns the new length /// </summary> /// <param name="target">The target list to apply this method on.</param> /// <param name="elements">The elements to add.</param> /// <returns>The new length</returns> public int UnShift(LObject target, params object[] elements) { var list = target.GetValue() as IList; if (list == null) return 0; if (elements == null || elements.Length == 0) return list.Count; for (var ndx = 0; ndx < elements.Length; ndx++) { object val = elements[ndx]; list.Insert(0, val); } return list.Count; }
/// <summary> /// Adds/Removes elements from an array /// </summary> /// <param name="target">The target list to apply this method on.</param> /// <param name="index">The index position to add/remove</param> /// <param name="howmany">How many elements to remove, if 0 no elements are removed</param> /// <param name="elements">Optional: The elements to add</param> /// <returns></returns> public LObject Splice(LObject target, int index, int howmany, params object[] elements) { var list = target.GetValue() as IList; List<object> removed = null; if (howmany > 0) { removed = new List<object>(); for (int ndxRemove = index; ndxRemove < (index + howmany); ndxRemove++) { removed.Add(list[ndxRemove]); } RemoveRange(list, index, howmany); } if (elements != null && elements.Length > 0 ) { var lastIndex = index; for (var ndx = 0; ndx < elements.Length; ndx++) { object objToAdd = elements[ndx]; list.Insert(lastIndex, objToAdd); lastIndex++; } } return new LArray(removed); }
/// <summary> /// Selects a part of an array, and returns the new array /// </summary> /// <param name="target">The target list to apply this method on.</param> /// <param name="start">The start of the selection</param> /// <param name="end">The end of the selection, if not supplied, selects all elements from start to end of the array</param> /// <returns>A new array</returns> public LObject Slice(LObject target, int start, int end) { var list = target.GetValue() as IList; var items = new List<object>(); if (end == -1) end = list.Count; for (var ndx = start; ndx < end; ndx++) items.Add(list[ndx]); return new LArray(items); }
/// <summary> /// Removes the first element of an array, and returns that element /// </summary> /// <param name="target">The target list to apply this method on.</param> /// <returns>The first element</returns> public object Shift(LObject target) { var list = target.GetValue() as IList; if (list.Count == 0) return null; object item = list[0]; list.RemoveAt(0); return item; }
/// <summary> /// Get / set value by index. /// </summary> /// <param name="target">The object whose index value is being set.</param> /// <param name="index">The index position to set the value</param> /// <param name="val">The value to set at the index</param> /// <returns></returns> public override void SetByNumericIndex(LObject target, int index, LObject val) { if (target == null) return; var list = target.GetValue() as IList; if (list == null || list.Count == 0) return; if (index < 0 || index >= list.Count) throw new IndexOutOfRangeException("Index : " + index); list[index] = val; }
/// <summary> /// Reverses the order of the elements in an array /// </summary> /// <param name="target">The target list to apply this method on.</param> /// <returns></returns> public LObject Reverse(LObject target) { var list = target.GetValue() as IList; int length = list.Count; if (length == 0 || length == 1) return null; // 2 or more. int highIndex = length - 1; int stopIndex = length / 2; if (length % 2 == 0) stopIndex--; for (int lowIndex = 0; lowIndex <= stopIndex; lowIndex++) { object tmp = list[lowIndex]; list[lowIndex] = list[highIndex]; list[highIndex] = tmp; highIndex--; } return target; }
/// <summary> /// Sets the property value for the specified propertyname. /// </summary> /// <param name="target">The object to set the property value on</param> /// <param name="propName">The name of the property</param> /// <param name="val">The value to set on the property</param> /// <returns></returns> public override void SetProperty(LObject target, string propName, object val) { var map = target.GetValue() as IDictionary; map[propName] = val; }
/// <summary> /// Removes the last element of an array, and returns that element /// </summary> /// <param name="target">The target list to apply this method on.</param> /// <returns>The removed element</returns> public object Pop(LObject target) { var list = target.GetValue() as IList; var index = list.Count - 1; object toRemove = list[index]; list.RemoveAt(index); return toRemove; }
/// <summary> /// Lenght of the array. /// </summary> /// <param name="target">The target list to apply this method on.</param> public int Length(LObject target) { var list = target.GetValue() as IList; return list.Count; }
/// <summary> /// Joins two or more arrays, and returns a copy of the joined arrays /// </summary> /// <param name="target">The target list to apply this method on.</param> /// <param name="item">The item to search for</param> /// <param name="start">The starting position of the search.</param> /// <returns>A copy of joined array</returns> public int LastIndexOf(LObject target, object item, int start) { var list = target.GetValue() as IList; var foundPos = -1; var total = list.Count; for(var ndx = start; ndx < total; ndx++) { var itemAt = list[ndx] as LObject; if (itemAt != null && itemAt.GetValue() == item) { foundPos = ndx; } } return foundPos; }
/// <summary> /// Joins all elements of an array into a string /// </summary> /// <param name="target">The target list to apply this method on.</param> /// <param name="separator">The separator to use for joining the elements.</param> /// <returns></returns> public string Join(LObject target, string separator = ",") { var list = target.GetValue() as IList; if (list == null || list.Count == 0) return string.Empty; var buffer = new StringBuilder(); var total = list.Count; var lobj = list[0] as LObject; if (lobj != null) buffer.Append(lobj.GetValue()); if (total > 1) { for (int ndx = 1; ndx < list.Count; ndx++) { lobj = list[ndx] as LObject; buffer.Append(separator + lobj.GetValue()); } } string result = buffer.ToString(); return result; }
/// <summary> /// Lenght of the array. /// </summary> /// <param name="target">The target list to apply this method on.</param> public int Length(LObject target) { var map = target.GetValue() as IDictionary; return(map.Count); }
/// <summary> /// Lenght of the array. /// </summary> /// <param name="target">The target list to apply this method on.</param> public int Length(LObject target) { var list = target.GetValue() as IList; return(list.Count); }
/// <summary> /// Adds new elements to the end of an array, and returns the new length /// </summary> /// <param name="target">The target list to apply this method on.</param> /// <param name="elements">The elements to add</param> /// <returns>The new length</returns> public int Push(LObject target, params object[] elements) { if (elements == null || elements.Length == 0) return 0; // Add var list = target.GetValue() as IList; foreach (object elem in elements) { var langType = LangTypeHelper.ConvertToLangValue(elem); list.Add(langType); } return list.Count; }
/// <summary> /// Adds new elements to the end of an array, and returns the new length /// </summary> /// <param name="target">The target list to apply this method on.</param> /// <param name="elements">The elements to add</param> /// <returns>The new length</returns> public int Push(LObject target, params object[] elements) { if (elements == null || elements.Length == 0) return 0; // Add var list = target.GetValue() as IList; if (list == null) return 0; foreach (object elem in elements) { if(list.GetType().IsGenericType) { var gt = list.GetType().GetGenericArguments()[0]; if(gt != null && gt.FullName.StartsWith("ComLib.Lang")) { var langVal = LangTypeHelper.ConvertToLangValue(elem); list.Add(langVal); } else { list.Add(elem); } } else { var langType = LangTypeHelper.ConvertToLangValue(elem); list.Add(langType); } } return list.Count; }
/// <summary> /// Get / set value by index. /// </summary> /// <param name="target">The target list to apply this method on.</param> /// <param name="index"></param> /// <returns></returns> public override object GetByNumericIndex(LObject target, int index) { if (target == null) return LObjects.Null; var list = target.GetValue() as IList; if (list == null || list.Count == 0) return LObjects.Null; if (index < 0 || index >= list.Count) throw new IndexOutOfRangeException("Index : " + index); return list[index]; }
/// <summary> /// Gets the property value for the specified propertyname. /// </summary> /// <param name="target">The object containing the property</param> /// <param name="propName">The name of the property</param> /// <returns></returns> public override object GetProperty(LObject target, string propName) { var map = target.GetValue() as IDictionary; return(map[propName]); }