PropertyObject.ts

  1. import { isString } from "@daybrush/utils";
  2. import { PropertyObjectState } from "./types";
  3. /**
  4. * Make string, array to PropertyObject for the dot product
  5. */
  6. class PropertyObject implements PropertyObjectState {
  7. public value: any[];
  8. public prefix: string = "";
  9. public suffix: string = "";
  10. public model: string = "";
  11. public type: string = "";
  12. public separator: string = ",";
  13. /**
  14. * @param - This value is in the array format.
  15. * @param - options
  16. * @example
  17. var obj = new PropertyObject([100,100,100,0.5], {
  18. "separator" : ",",
  19. "prefix" : "rgba(",
  20. "suffix" : ")"
  21. });
  22. */
  23. constructor(value: string | any[], options?: Partial<PropertyObjectState>) {
  24. options && this.setOptions(options);
  25. this.value = isString(value) ? value.split(this.separator) : value;
  26. }
  27. public setOptions(newOptions: Partial<PropertyObjectState>) {
  28. for (const name in newOptions) {
  29. this[name as keyof PropertyObjectState] = newOptions[name as keyof PropertyObjectState];
  30. }
  31. return this;
  32. }
  33. /**
  34. * the number of values.
  35. * @example
  36. const obj1 = new PropertyObject("1,2,3", ",");
  37. console.log(obj1.length);
  38. // 3
  39. */
  40. public size() {
  41. return this.value.length;
  42. }
  43. /**
  44. * retrieve one of values at the index
  45. * @param {Number} index - index
  46. * @return {Object} one of values at the index
  47. * @example
  48. const obj1 = new PropertyObject("1,2,3", ",");
  49. console.log(obj1.get(0));
  50. // 1
  51. */
  52. public get(index: number) {
  53. return this.value[index];
  54. }
  55. /**
  56. * Set the value at that index
  57. * @param {Number} index - index
  58. * @param {Object} value - text, a number, object to set
  59. * @return {PropertyObject} An instance itself
  60. * @example
  61. const obj1 = new PropertyObject("1,2,3", ",");
  62. obj1.set(0, 2);
  63. console.log(obj1.toValue());
  64. // 2,2,3
  65. */
  66. public set(index: number, value: any) {
  67. this.value[index] = value;
  68. return this;
  69. }
  70. /**
  71. * create a copy of an instance itself.
  72. * @return {PropertyObject} clone
  73. * @example
  74. const obj1 = new PropertyObject("1,2,3", ",");
  75. const obj2 = obj1.clone();
  76. */
  77. public clone(): PropertyObject {
  78. const {
  79. separator,
  80. prefix,
  81. suffix,
  82. model,
  83. type,
  84. } = this;
  85. const arr = this.value.map(v => ((v instanceof PropertyObject) ? v.clone() : v));
  86. return new PropertyObject(arr, {
  87. separator,
  88. prefix,
  89. suffix,
  90. model,
  91. type,
  92. });
  93. }
  94. /**
  95. * Make Property Object to String
  96. * @return {String} Make Property Object to String
  97. * @example
  98. //rgba(100, 100, 100, 0.5)
  99. const obj4 = new PropertyObject([100,100,100,0.5], {
  100. "separator" : ",",
  101. "prefix" : "rgba(",
  102. "suffix" : ")",
  103. });
  104. console.log(obj4.toValue());
  105. // "rgba(100,100,100,0.5)"
  106. */
  107. public toValue(): string {
  108. return this.prefix + this.join() + this.suffix;
  109. }
  110. /**
  111. * Make Property Object's array to String
  112. * @return {String} Join the elements of an array into a string
  113. * @example
  114. //rgba(100, 100, 100, 0.5)
  115. var obj4 = new PropertyObject([100,100,100,0.5], {
  116. "separator" : ",",
  117. "prefix" : "rgba(",
  118. "suffix" : ")"
  119. });
  120. obj4.join(); // => "100,100,100,0.5"
  121. */
  122. public join() {
  123. return this.value.map(v => ((v instanceof PropertyObject) ? v.toValue() : v)).join(this.separator);
  124. }
  125. /**
  126. * executes a provided function once per array element.
  127. * @param {Function} callback - Function to execute for each element, taking three arguments
  128. * @param {All} [callback.currentValue] The current element being processed in the array.
  129. * @param {Number} [callback.index] The index of the current element being processed in the array.
  130. * @param {Array} [callback.array] the array.
  131. * @return {PropertyObject} An instance itself
  132. * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach|MDN Array.forEach()} reference to MDN document.
  133. * @example
  134. //rgba(100, 100, 100, 0.5)
  135. var obj4 = new PropertyObject([100,100,100,0.5], {
  136. "separator" : ",",
  137. "prefix" : "rgba(",
  138. "suffix" : ")"
  139. });
  140. obj4.forEach(t => {
  141. console.log(t);
  142. }); // => "100,100,100,0.5"
  143. */
  144. public forEach(func: (value?: any, index?: number, array?: any[]) => void) {
  145. this.value.forEach(func);
  146. return this;
  147. }
  148. }
  149. export default PropertyObject;