Given an object and a field selector, do the following:
- If run in debug mode:
- Verify whether all wanted fields exist within the object.
- Return a filtered objects - only the wanted fields will be included.
- If run in non-debug mode:
- Simply return the given object.
This might be useful for documenting and testing interface requirements (see use case below).
var x = $.usosUtils.requireFields({a: {b: 3, c: 2}, b: 1, c: 1}, "a[c]|c");
/* Now x equals {a: {c: 2}, c: 1}. */
If your function takes a complex object as a parameter, it might be useful to
make it a habit of calling requireFields
in the beginning of such complex
functions:
function foo(user) {
user = $.usosUtils.requireFields(obj, "first_name|last_name|student_number");
/* ... */
};
-
Useful for documenting: If you use
user.pesel
infoo
, it will beundefined
, until you addpesel
to yourrequirements
. Hence, it forces you to update yourrequirements
properly. -
Useful for testing: If you forget to fetch
student_number
into youruser
, and then you callfoo(user)
, an error will be logged into the developer console (in debug mode). Hence, you should notice that your object does not meet your function's requirements.
object
- any object (usually fetched from USOS API).requirements
- a string in the same format as USOS API'sfields
parameter. You may require nested fields using brackets.