@yuneta/gobj-js 7.16.6 → 7.21.0

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.
@@ -4021,10 +4021,18 @@ Being `kw` a:
4021
4021
  - list of strings [s,...]
4022
4022
  - list of dicts [{},...]
4023
4023
  - dict of dicts {id:{},...}
4024
- return a **NEW** list of incref (clone) kw filtering the rows by `jn_filter` (where),
4025
- and matching the ids.
4026
- If match_fn is 0 then kw_match_simple is used.
4027
- NOTE Using JSON_INCREF/JSON_DECREF HACK
4024
+ return a NEW list holding the rows that match `ids` and the
4025
+ `jn_filter` (where). If match_fn is 0 then kw_match_simple is used.
4026
+
4027
+ WARNING the LIST is new; its elements are THE SAME objects, not
4028
+ copies. The C twin increfs each one and says "clone", which this
4029
+ comment repeated for years: javascript has no refcount, so a write
4030
+ through a collected row writes into `kw`. Copy what you mean to
4031
+ modify.
4032
+
4033
+ Returns null -- not an empty list -- for a `kw` that is neither a
4034
+ list nor a dict, so a caller that reads `.length` off the answer
4035
+ must check it first (kwid_find_one_record does).
4028
4036
  *************************************************************/
4029
4037
  function kwid_collect(gobj, kw, ids, jn_filter, match_fn) {
4030
4038
  if (gobj && !is_gobj(gobj)) log_error(`GObj bad instanceof`);
@@ -4053,12 +4061,17 @@ function kwid_collect(gobj, kw, ids, jn_filter, match_fn) {
4053
4061
  }
4054
4062
  /*************************************************************
4055
4063
  Utility for databases.
4056
- Return a new dict from a "dict of records" or "list of records"
4057
- WARNING the "id" of a dict's record is hardcorded to their key.
4064
+ Return a dict from a "dict of records" or "list of records".
4065
+ WARNING the "id" of a dict's record is hardcoded to its key.
4058
4066
  Convention:
4059
- - all arrays are list of records (dicts) with "id" field as primary key
4067
+ - all arrays are lists of records (dicts) with "id" as primary key
4060
4068
  - delimiter is '`' and '.'
4061
4069
  If path is empty then use kw
4070
+
4071
+ WARNING "new" is the C twin's word, where it means a new REFERENCE:
4072
+ handed a dict, both return THAT dict and not a copy of it. Only the
4073
+ list case builds something new, and even then the records inside it
4074
+ are the same objects.
4062
4075
  *************************************************************/
4063
4076
  function kwid_new_dict(gobj, kw, path) {
4064
4077
  if (gobj && !is_gobj(gobj)) log_error(`GObj bad instanceof`);
@@ -4067,20 +4080,59 @@ function kwid_new_dict(gobj, kw, path) {
4067
4080
  if (is_object(kw)) new_dict = kw;
4068
4081
  else if (is_array(kw)) for (let i = 0; i < kw.length; i++) {
4069
4082
  let kv = kw[i];
4070
- let id = kw_get_str(gobj, kv, "id", "", 0);
4083
+ let id = kw_get_str(gobj, kv, "id", "", kw_flag_t.KW_REQUIRED);
4071
4084
  if (!empty_string(id)) new_dict[id] = kv;
4072
4085
  }
4073
4086
  else log_error(`${gobj_short_name(gobj)} kwid_new_dict: data type unknown`);
4074
4087
  return new_dict;
4075
4088
  }
4076
4089
  /*************************************************************
4090
+ Utility for databases.
4091
+ Return a list from a "dict of records" or "list of records".
4092
+ The NORMALIZING function of this family: whatever shape the data
4093
+ arrives in, what comes back is the shape a table indexed and
4094
+ sorted by `id` wants.
4095
+ Convention:
4096
+ - all arrays are lists of records (dicts) with "id" as primary key
4097
+ - delimiter is '`' and '.'
4098
+ If path is empty then use kw
4099
+
4100
+ WARNING handed a DICT it writes each record's key into the record
4101
+ as its `id`, OVERWRITING one that differs -- in the source records,
4102
+ which it does not copy. That is the C twin's behaviour
4103
+ (`json_object_set_new(v, "id", ...)` in kwid.c) and it is the point:
4104
+ a dict keyed by id whose records disagree with their own key is
4105
+ exactly what this is for. Handed a LIST it returns that same list,
4106
+ untouched, as C returns a new reference to it.
4107
+ *************************************************************/
4108
+ function kwid_new_list(gobj, kw, path) {
4109
+ if (gobj && !is_gobj(gobj)) log_error(`GObj bad instanceof`);
4110
+ if (!empty_string(path)) kw = kw_find_path(gobj, kw, path);
4111
+ if (is_array(kw)) return kw;
4112
+ if (is_object(kw)) {
4113
+ let new_list = [];
4114
+ for (let id of Object.keys(kw)) {
4115
+ let record = kw[id];
4116
+ if (!is_object(record)) {
4117
+ log_error(`${gobj_short_name(gobj)} kwid_new_list: not a record: '${id}'`);
4118
+ continue;
4119
+ }
4120
+ record["id"] = id;
4121
+ new_list.push(record);
4122
+ }
4123
+ return new_list;
4124
+ }
4125
+ log_error(`${gobj_short_name(gobj)} kwid_new_list: wrong type for list`);
4126
+ return [];
4127
+ }
4128
+ /*************************************************************
4077
4129
  * Utility for databases. See kwid_collect parameters
4078
4130
  *************************************************************/
4079
4131
  function kwid_find_one_record(gobj, kw, ids, jn_filter, match_fn) {
4080
4132
  if (gobj && !is_gobj(gobj)) log_error(`GObj bad instanceof`);
4081
4133
  let list = kwid_collect(gobj, kw, ids, jn_filter, match_fn);
4082
- if (list.length > 0) return list[0];
4083
- else return null;
4134
+ if (!list || list.length === 0) return null;
4135
+ return list[0];
4084
4136
  }
4085
4137
  /*************************************************************
4086
4138
  Utility for databases.
@@ -7128,6 +7180,7 @@ exports.kwid_find_one_record = kwid_find_one_record;
7128
7180
  exports.kwid_get_ids = kwid_get_ids;
7129
7181
  exports.kwid_match_id = kwid_match_id;
7130
7182
  exports.kwid_new_dict = kwid_new_dict;
7183
+ exports.kwid_new_list = kwid_new_list;
7131
7184
  exports.list2options = list2options;
7132
7185
  exports.load_json_file = load_json_file;
7133
7186
  exports.log_debug = log_debug;