|
1 | | -# common |
2 | | -Common utilities for jsutils library |
| 1 | +# jsutilslib common utilities |
| 2 | +This is a set of javascript helper classes and functions for javascript applications. |
| 3 | + |
| 4 | +_jsutilslib_ can be used by themselves in your web applications, but they are also part of [`jsutilslib`](https://github.com/jsutilslib/jsutilslib), which is a library that consists of a set of curated components, utility functions, clases, etc. that are flexible enough to be re-used in different javascript applications. |
| 5 | + |
| 6 | +> Some parts of the library are intended to be used in conjunction with jQuery, and so it is advisable to include jQuery in your project prior to including jsutilslib. |
| 7 | +
|
| 8 | +## Common utilities |
| 9 | +The library consists of the next utilities: |
| 10 | + |
| 11 | +- tag: enables to create html elements using a notation similar to pug or jade. |
| 12 | +- processprops: processes the properties of an object by applying a function to each of them; it is also able to clone the object. |
| 13 | +- clone: is a shorthand for processprops that forces cloning the object. |
| 14 | +- merge: merges two objects into a single one, but only those properties that are part of the first one. |
| 15 | +- Array._trim: appends a function to the Array prototype that removes the empty elements from an array. |
| 16 | +- Element._append: is a proxy to Element.append() function that returns the element thus enabling to chain multiple actions on the object. |
| 17 | + |
| 18 | +## Using |
| 19 | + |
| 20 | +There are a set of _javascript_ files that contain a part of the library, each one (in folder `js`). These files can be used individually or combined into a single one, by concatenating them (or by using `uglify-js`). |
| 21 | + |
| 22 | +A `Makefile` is provided to create the single all-in-one `js` files for the library. |
| 23 | + |
| 24 | +```console |
| 25 | +# npm install -g uglify-js |
| 26 | +... |
| 27 | +# git clone https://github.com/jsutilslib/common |
| 28 | +# cd common |
| 29 | +# make |
| 30 | +uglifyjs js/*.js -b | cat notice - > common.js |
| 31 | +uglifyjs js/*.js | cat notice.min - > common.min.js |
| 32 | +``` |
| 33 | + |
| 34 | +Now you can use files `common.min.js`: |
| 35 | + |
| 36 | +```html |
| 37 | +<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> |
| 38 | +<script src="common.min.js"></script> |
| 39 | +``` |
| 40 | + |
| 41 | +### From a CDN |
| 42 | + |
| 43 | +It is possible to use `common` directly from a CDN: |
| 44 | + |
| 45 | +```html |
| 46 | +<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> |
| 47 | +<script src="https://cdn.jsdelivr.net/gh/jsutilslib/common@1.0.0-beta/common.min.js"></script> |
| 48 | +``` |
| 49 | + |
| 50 | +## Documentation |
| 51 | + |
| 52 | +### Tag |
| 53 | + |
| 54 | +This is a helper function, used to create html objects from pug-like object description. |
| 55 | + |
| 56 | +```javascript |
| 57 | +jsutilslib.tag('p#id1.class1.class2'); |
| 58 | +``` |
| 59 | + |
| 60 | +will create a tag like |
| 61 | + |
| 62 | +```html |
| 63 | +<p id="id1" class="class1 class2"></p> |
| 64 | +``` |
| 65 | + |
| 66 | +The prototype of `tag` function is |
| 67 | + |
| 68 | +```javascript |
| 69 | +jsutilslib.tag(htmldescription, text) |
| 70 | +jsutilslib.tag(htmldescription, properties, text) |
| 71 | +``` |
| 72 | + |
| 73 | +The parameter description is the next: |
| 74 | +- _htmldescription_: is the description of the object, using the notation `[tag name][#id][.class1][.class2]...[.classN]`. If ommited, the tag name will be considered as `div`; if ommited the other parts, they will not be included in the html tag. |
| 75 | +- _text_: is the text content for the object. |
| 76 | +- _properties_: is a dictionary of attributes that will appear in the html tag. |
| 77 | + |
| 78 | +**Examples** |
| 79 | + |
| 80 | +- `jsutilslib.tag("p.small", "New tag")` creates `<p class="small">New tag</p>` |
| 81 | +- `jsutilslib.tag(".selectable", {style: "width: 100%"})` creates `<div class="selectable" style="width: 100%;"></div>` |
| 82 | +- `jsutilslib.tag("#selection.selectable")` creates `<div id="selection" class="selectable"></div>` |
| 83 | + |
| 84 | + |
| 85 | +### processprops |
| 86 | + |
| 87 | +`processprops` is a function that processes the properties of an object, by applying a function to each of them. |
| 88 | + |
| 89 | +The prototype of `processprops` of the function is the next: |
| 90 | + |
| 91 | +```javascript |
| 92 | +function processprops(target, objectfnc = v => v, clone = false) |
| 93 | +``` |
| 94 | + |
| 95 | +Where |
| 96 | +- _target_: is the object whose properties are to be processed. |
| 97 | +- _objectfnc_: is the function to apply to each property. The prototype is `function(value, name_of_propery, object)`. |
| 98 | +- _clone_: if true, a new object will be |
| 99 | + |
| 100 | +Let's explain it by an example: |
| 101 | + |
| 102 | +```javascript |
| 103 | +let obj = { a:1, b:2, c:3 }; |
| 104 | +let obj1 = jsutilslib.processprops(obj, (x) => (x + 1)); |
| 105 | +``` |
| 106 | + |
| 107 | +In this example, the function `(x) => (x + 1)` is applied to each property, and the result is the next: |
| 108 | + |
| 109 | +```javascript |
| 110 | +{a: 2, b: 3, c: 4} |
| 111 | +``` |
| 112 | + |
| 113 | +As we did not clone the object (parameter clone set to `false` by default), both obj and obj1 objects are the same. Then if we change a property in obj1, it will be reflected in obj: |
| 114 | + |
| 115 | +```javascript |
| 116 | +$ obj1.a = 10 |
| 117 | +10 |
| 118 | +$ obj |
| 119 | +{a: 10, b: 3, c: 4} |
| 120 | +``` |
| 121 | + |
| 122 | +If we wanted each object to be different, we could clone the object: |
| 123 | + |
| 124 | +```javascript |
| 125 | +$ obj = { a:1, b:2, c:3 }; |
| 126 | +$ let obj2 = jsutilslib.processprops(obj, (x) => (x + 1), true) |
| 127 | +{a: 2, b: 3, c: 4} |
| 128 | +``` |
| 129 | + |
| 130 | +And if we modify obj2, we'll see that obj keeps its state: |
| 131 | + |
| 132 | +```javascript |
| 133 | +$ obj2.a = 20 |
| 134 | +20 |
| 135 | +$ obj |
| 136 | +{a: 1, b: 2, c: 3} |
| 137 | +$ obj2 |
| 138 | +{a: 20, b: 3, c: 4} |
| 139 | +``` |
| 140 | + |
| 141 | +The function also works with arbitrary object and arbitrary processing functions. |
| 142 | + |
| 143 | +```javascript |
| 144 | +$ class TestClass { |
| 145 | + constructor (a) { |
| 146 | + this.a = a; |
| 147 | + } |
| 148 | +} |
| 149 | +$ a1 = new TestClass("simpleA") |
| 150 | +TestClass {a: 'simpleA'} |
| 151 | + a: "simpleA" |
| 152 | + [[Prototype]]: Object |
| 153 | +$ a2 = jsutilslib.processprops(a1, (x) => new TestClass(x)) |
| 154 | +TestClass {a: TestClass} |
| 155 | + a: TestClassa: "simpleA" |
| 156 | + [[Prototype]]: Object |
| 157 | + [[Prototype]]: Object |
| 158 | +``` |
| 159 | + |
| 160 | +Even it works when cloning the object |
| 161 | + |
| 162 | +```javascript |
| 163 | +$ a3 = new TestClass("simpleA") |
| 164 | +TestClass {a: 'simpleA'} |
| 165 | + a: "simpleA" |
| 166 | + [[Prototype]]: Object |
| 167 | +$ a4 = jsutilslib.processprops(a3, (x) => new TestClass(`independent ${x}`), true) |
| 168 | +TestClass {a: TestClass} |
| 169 | + a: TestClass |
| 170 | + a: "independent simpleA" |
| 171 | + [[Prototype]]: Object |
| 172 | + [[Prototype]]: Object |
| 173 | +``` |
| 174 | + |
| 175 | +### clone |
| 176 | + |
| 177 | +Function `clone` is a shorthand for `processprops` which clones the object and its properties. The prototype is the next |
| 178 | + |
| 179 | +```javascript |
| 180 | +function clone(target, objectfnc = x => clone(x)) |
| 181 | +``` |
| 182 | + |
| 183 | +### merge |
| 184 | + |
| 185 | +This function walks over the properties of one object and sets the properties to the values of the properties with the same name in other project. The function returns a different object. This is useful for e.g. creating settings from default values and user provided ones. |
| 186 | + |
| 187 | +```javascript |
| 188 | +$ defaults = { |
| 189 | + classdragging: 'grabbing', |
| 190 | + callbackstart: function() {}, |
| 191 | + callbackend: function() {}, |
| 192 | + callbackmove: function(dx, dy) {} |
| 193 | +} |
| 194 | +$ settings = jsutilslib.merge(defaults, { classdragging: "dragging" }); |
| 195 | +``` |
| 196 | + |
| 197 | +### Array._trim |
| 198 | + |
| 199 | +Function `_trim` is appended to the prototype of class Array. This function removes the _empty elements_ from the array (those that evaluate to a string equal to ""). |
| 200 | + |
| 201 | +```javascript |
| 202 | +$ a = ['a', 'b', '', 'd' ] |
| 203 | +$ a._trim() |
| 204 | +['a', 'b', 'd'] |
| 205 | +``` |
| 206 | + |
| 207 | +### Element._append |
| 208 | + |
| 209 | +Function `_append` is appended to the prototype of class Element. This function simply calls function `Element.append`, but also returns the object in order to enable function chaining. |
| 210 | + |
| 211 | +```javascript |
| 212 | +$ document.querySelector('body')._append(jsutilslib.tag("p", "content"))._append(jsutilslib.tag("p", "more content")) |
| 213 | +``` |
| 214 | + |
0 commit comments