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