@lumjs/core 1.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/prop.js ADDED
@@ -0,0 +1,170 @@
1
+
2
+ const {isObj,isNil,notNil,isProperty,unbound,F} = require('./types');
3
+ const {DESC,getDescriptor} = require('./descriptors');
4
+ const {cloneIfLocked} = require('./obj/clone');
5
+
6
+ /**
7
+ * A magic wrapper for Object.defineProperty()
8
+ *
9
+ * @method Lum.prop
10
+ *
11
+ * More in-depth than the `def()` function, this has a bazillion different
12
+ * features, and will continue to evolve, while the simpler function is done.
13
+ *
14
+ * Rather than documenting the arguments in the usual manner, I'm
15
+ * going to simply show all of the ways this method can be called.
16
+ *
17
+ * Anywhere the `target` parameter is shown, this parameter can be an `object` or `function`.
18
+ * It's the target to which we're adding new properties.
19
+ *
20
+ * Anywhere the `property` parameter is shown, this parameter can be specified in two different
21
+ * forms. The first and simplest is as a `string` in which case it's simply the name of the property we're adding.
22
+ * The second more advanced form is as an `object`. If it is specified as an object, then it is a set of special options.
23
+ * In this case, a property of that `property` object called `name` will be used as the name of the property.
24
+ * If the `name` property is absent or `undefined`, it's the same as not passing the `property` parameter at all,
25
+ * and a *bound* function will be returned, using the custom options as its bound defaults.
26
+ *
27
+ * See below the usage
28
+ *
29
+ * `Lum.prop(object)`
30
+ *
31
+ * Return a function that is a bound copy of this function with
32
+ * the object as it's first parameter.
33
+ *
34
+ * `Lum.prop(object, property)`
35
+ *
36
+ * Add a property to the object which is mapped to a bound copy of
37
+ * this function with the object as it's first parameter.
38
+ *
39
+ * `Lum.prop(object, property, function, function)`
40
+ *
41
+ * Add a getter and setter property with the default descriptor.
42
+ *
43
+ * `Lum.prop(object, property, function, function, object)`
44
+ *
45
+ * Add a getter and setter property with specified Descriptor options.
46
+ * Do not use `get`, `set`, or `value` in the descriptor options.
47
+ *
48
+ * `Lum.prop(object, property, function, null, object)`
49
+ *
50
+ * Add a getter only with specified descriptor options.
51
+ * Same restrictions to the descriptor options as above.
52
+ * You can specify `{}` as the descriptor options to use the defaults.
53
+ *
54
+ * `Lum.prop(object, property, null, function, object)`
55
+ *
56
+ * Add a setter only with specified descriptor options.
57
+ * Same restrictions as above, and again you can use `{}` for defaults.
58
+ *
59
+ * `Lum.prop(object, property, !null)`
60
+ *
61
+ * Add a property with the specified non-null value.
62
+ * This uses the default descriptor.
63
+ *
64
+ * `Lum.prop(object, property, !null, object)`
65
+ *
66
+ * Add a property value with the specified descriptor options.
67
+ * Has the same restrictions to the descriptor options as above.
68
+ *
69
+ * `Lum.prop(object, property, null, object)`
70
+ *
71
+ * Add a property using the descriptor object alone.
72
+ * Use the newer and shorter `def()` function instead.
73
+ *
74
+ * `Lum.prop(object, property, Descriptor)`
75
+ *
76
+ * If you use `DESC.make()` or `descriptor()` to build a magic
77
+ * descriptor object, you can pass it and it'll be used with
78
+ * a few bonus features I need to document that aren't supported by
79
+ * the otherwise equivalent `def(object,property,descriptor)` call.
80
+ *
81
+ */
82
+ function prop(obj, name, arg1, arg2, arg3)
83
+ {
84
+ let opts;
85
+
86
+ const isUnbound = unbound(this, true, true);
87
+
88
+ if (isObj(name))
89
+ { // A way to set some special options.
90
+ opts = name;
91
+ name = opts.name;
92
+ }
93
+ else if (isUnbound)
94
+ { // Use the default options.
95
+ opts = isObj(prop.options) ? prop.options : {};
96
+ }
97
+ else if (isObj(this))
98
+ { // This is already a bound copy, so `this` is the options.
99
+ opts = this;
100
+ }
101
+ else
102
+ { // Something weird is going on here...
103
+ throw new Error("Invalid `this` in a prop() function call");
104
+ }
105
+
106
+ if (isNil(name))
107
+ { // A special case, returns a copy of this function bound to the object.
108
+ return prop.bind(opts, obj);
109
+ }
110
+ else if (!isProperty(name))
111
+ { // The property must in every other case be a string.
112
+ throw new Error("property name must be a string or Symbol");
113
+ }
114
+
115
+ let desc;
116
+
117
+ if (arg1 === undefined && arg2 === undefined)
118
+ { // Another special case, the property is a bound version of this.
119
+ return prop(obj, name, prop.bind(opts, obj));
120
+ }
121
+ else if (DESC.is(arg1) && arg2 === undefined)
122
+ { // Yet another special case.
123
+ if (arg1.isReady)
124
+ { // Already has a value or get/set properties assigned.
125
+ desc = arg1;
126
+ }
127
+ else
128
+ { // We'll need to call setValue(), setGetter(), etc, then done().
129
+ return arg1.whenDone(function()
130
+ {
131
+ return prop(obj, name, this);
132
+ });
133
+ }
134
+ }
135
+ else if (typeof arg1 === F && typeof arg2 === F)
136
+ { // A getter and setter were specified.
137
+ desc = getDescriptor(isObj(arg3) ? cloneIfLocked(arg3) : DESC.CONF);
138
+ desc.setAccessor(arg1, arg2);
139
+ }
140
+ else if (isObj(arg3))
141
+ { // A custom descriptor for an accessor, find the accessor.
142
+ desc = getDescriptor(cloneIfLocked(arg3));
143
+ if (typeof arg1 === F)
144
+ { // A getter-only accessor.
145
+ desc.setGetter(arg1);
146
+ }
147
+ else if (typeof arg2 === F)
148
+ { // A setter-only accessor.
149
+ desc.setSetter(arg2);
150
+ }
151
+ }
152
+ else
153
+ { // Not a getter/setter, likely a standard value.
154
+ desc = getDescriptor(isObj(arg2) ? cloneIfLocked(arg2) : DESC.CONF);
155
+
156
+ if (notNil(arg1))
157
+ { // If you really want a null 'value', use a custom descriptor.
158
+ desc.setValue(arg1);
159
+ }
160
+ }
161
+
162
+ // TODO:
163
+ // - Add different configurable return values.
164
+ // - Make default return values consistent with def().
165
+
166
+ // If we reached here, we should have a valid descriptor now.
167
+ return Object.defineProperty(obj, name, desc);
168
+ }
169
+
170
+ module.exports = prop;
package/src/strings.js ADDED
@@ -0,0 +1,76 @@
1
+ // String methods.
2
+ const {isObj,root} = require('./types')
3
+
4
+ /**
5
+ * Get the locale/language string.
6
+ *
7
+ * 1. If `navigator.language` exists it will be used.
8
+ * 2. If `Intl` exists it will be used.
9
+ * 3. If neither of those exist, uses `'en-US'` as a default.
10
+ *
11
+ * @returns string - The locale/language string.
12
+ */
13
+ function getLocale()
14
+ {
15
+ if (isObj(root.navigator) && typeof root.navigator.language === S)
16
+ {
17
+ return root.navigator.language;
18
+ }
19
+ else if (isObj(root.Intl))
20
+ {
21
+ try
22
+ {
23
+ const lang = root.Intl.DateTimeFormat().resolvedOptions().locale;
24
+ return lang;
25
+ }
26
+ catch (err)
27
+ {
28
+ console.warn("Attempt to get locale from Intl failed", err);
29
+ }
30
+ }
31
+
32
+ // A last-ditch fallback.
33
+ return 'en-US';
34
+ }
35
+
36
+ exports.getLocale = getLocale;
37
+
38
+ /**
39
+ * Make the first character of a string uppercase.
40
+ *
41
+ * @param {string} string - The input string.
42
+ * @param {boolean} [lcrest=false] Make the rest of the string lowercase?
43
+ * @param {string} [locale=getLocale()] The locale/language of the string.
44
+ *
45
+ * @returns string - The output string.
46
+ */
47
+ function ucfirst ([ first, ...rest ], lcrest = false, locale = getLocale())
48
+ {
49
+ first = first.toLocaleUpperCase(locale);
50
+ rest = rest.join('');
51
+ if (lcrest)
52
+ {
53
+ rest = rest.toLocaleLowerCase(locale);
54
+ }
55
+ return first + rest;
56
+ }
57
+
58
+ exports.ucfirst = ucfirst;
59
+
60
+ /**
61
+ * Make the first character of each *word* in a string uppercase.
62
+ *
63
+ * @param {string} string - The input string.
64
+ * @param {boolean} [unicode=false] Use Unicode words? (Only uses ASCII words otherwise)
65
+ * @param {boolean} [lcrest=false] Make the rest of each word lowercase?
66
+ * @param {string} [locale=getLocale()] The locale/language of the string.
67
+ *
68
+ * @returns {string} - The output string.
69
+ */
70
+ function ucwords(string, unicode = false, lcrest = false, locale = getLocale())
71
+ {
72
+ const regex = unicode ? /[0-9\p{L}]+/ug : /\w+/g;
73
+ return string.replace(regex, word => ucfirst(word, lcrest, locale));
74
+ }
75
+
76
+ exports.ucwords = ucwords;