ident
All repositories: gitoria
4.6 KB
// lib/notify-helpers.hl — what lib/notify.hl looks up: the checks of a kind name, a text and a URL, the time// stamp, the two orders (newest first, by name), on/off words. No table access. Statics only.import { timestamp } from 'hl:time'import { countOf, hasControl, urlChars } from './util.hl'static maxName = 60static maxText = 2000static maxUrl = 2000static bool = (v) => { return v == true }static onOff = (b) => { return b ? 'on' : 'off' }// "YYYY-MM-DD HH:MM UTC" (hl:time knows no zones, hybriel #11)static stamp = (ms) => {if (ms == null) { return '' }t = timestamp(ms)return t.slice(0, 10) + ' ' + t.slice(11, 16) + ' UTC'}// NEWEST FIRST by `created` (a tie by the id); insertion sort by hand (no list sort(), hybriel #6)static idAfter = (a, b) => {let i = 0while (i < a.length && i < b.length) {let x = a.charCodeAt(i)let y = b.charCodeAt(i)if (x != y) { return x > y }i = i + 1}return a.length > b.length}static newerThan = (a, b) => {ca = a.created == null ? 0 : a.createdcb = b.created == null ? 0 : b.createdif (ca != cb) { return ca > cb }return idAfter(a.id, b.id)}static newestFirst = (list) => {out = []if (countOf(list) == 0) { return out }for (x of list) { out.push(x) }let i = 1while (i < out.length) {let cur = out[i]let j = i - 1while (j >= 0 && newerThan(cur, out[j])) {out[j + 1] = out[j]j = j - 1}out[j + 1] = curi = i + 1}return out}// kinds by name, A-Z (char codes)static nameBefore = (a, b) => {x = a.name.toLowerCase()y = b.name.toLowerCase()let i = 0while (i < x.length && i < y.length) {if (x.charCodeAt(i) != y.charCodeAt(i)) { return x.charCodeAt(i) < y.charCodeAt(i) }i = i + 1}return x.length < y.length}static byName = (list) => {out = []if (countOf(list) == 0) { return out }for (x of list) { out.push(x) }let i = 1while (i < out.length) {let cur = out[i]let j = i - 1while (j >= 0 && nameBefore(cur, out[j])) {out[j + 1] = out[j]j = j - 1}out[j + 1] = curi = i + 1}return out}// ---- validation -------------------------------------------------------------------------// A KIND NAME / NOTIFICATION NAME: trimmed, 1-60 chars, one line. Answers { name } or { error }.static checkKindName = (v, what) => {if (v == null || hlTypeName(v) != 'String') { return { error = 'field ' + what + ' must be a string' } }t = v.trim()if (t == '') { return { error = 'field ' + what + ' is empty' } }if (t.length > maxName) { return { error = 'field ' + what + ' is longer than ' + maxName + ' characters' } }if (hasControl(t)) { return { error = 'field ' + what + ' contains a control character' } }return { name = t }}// THE TEXT: 1-2000 chars (Markdown by the worldapi convention; shown as plain text with// its line breaks); tab, line feed and carriage return are the only control charactersstatic checkText = (v) => {if (v == null || hlTypeName(v) != 'String') { return { error = 'field text must be a string' } }t = v.trim()if (t == '') { return { error = 'field text is empty' } }if (t.length > maxText) { return { error = 'field text is longer than ' + maxText + ' characters' } }let i = 0while (i < t.length) {let c = t.charCodeAt(i)if ((c < 32 && c != 9 && c != 10 && c != 13) || c == 127) { return { error = 'field text contains a control character' } }i = i + 1}return { text = t }}// AN ICON / ACTION LINK: an absolute http(s) URL, printable URL characters only, ≤ 2000static checkUrl = (v, what) => {if (v == null || hlTypeName(v) != 'String') { return { error = 'field ' + what + ' must be a string' } }t = v.trim()if (t.length > maxUrl) { return { error = 'field ' + what + ' is longer than ' + maxUrl + ' characters' } }let i = 0while (i < t.length) {if (!urlChars.includes(t[i])) { return { error = 'field ' + what + ' contains a character that is not allowed in a URL' } }i = i + 1}lower = t.toLowerCase()scheme = lower.slice(0, 7) == 'http://' ? 7 : (lower.slice(0, 8) == 'https://' ? 8 : 0)if (scheme == 0) { return { error = 'field ' + what + ' must be an http:// or https:// URL' } }rest = t.slice(scheme, t.length)if (rest == '' || rest[0] == '/' || rest[0] == '?') { return { error = 'field ' + what + ' has no host' } }return { url = t }}// a user's switch over the app's preset: 'on' / 'off', or '' = the presetstatic switchValue = (userValue, preset) => {if (userValue == 'on') { return true }if (userValue == 'off') { return false }return preset}static kindRowOut = (k) => { return { name = k.name push = bool(k.push) email = bool(k.email) } }static unreadCount = (rows) => {let n = 0for (r of rows) { if (r.unread) { n = n + 1 } }return n}
Branches
Latest commits
- bf1980f2Merge branch 'v56-day56' into mainIdent
- 62ae0adf|||PR ident: vendor Hybriel day56mre
- 0d18abc4Merge branch 'f13-avatar' into mainIdent
- 2a99730a|||PR ident#37 avatar in hlmre
- fa4ed25bMerge branch 'f11-upsert' into mainIdent
- 8f3d9bc9|||PR ident#42 keyed upsert and removemre
- 675493b1ident#42: editing an identity upserts the one edited row (face editIdentity answers { identity, row }); a create or delete keeps the whole list because the rows' position-dependent parts (default, Delete, Identity n) changemre
- 45e55303ident#42: an app write patches one row: upsert on create, edit and new secret, one-argument remove on delete; the faces answer the changed app (or the deleted id), not the whole listmre
- 991641d4Merge branch 'f10-access' into mainIdent
- e56d8273|||PR ident#34 part 4: access and inbox fixed classesmre
- df7696dbident#34: tests: access ticks selector quoting fixmre
- 7215903eident#34: access: the unstyled classes all, app, tick, apptick, plus are dropped; the gate finds the app ticks by input[name=app]mre
- 69b0a0ceident#34: access: the pick buttons use Style.pickCard instead of class pickmre
- dc702419Merge branch 'f9-choose' into mainIdent
- 8caec771|||PR ident#34 restore .choose hookmre
- 0dbff960Merge branch 'f8-shared' into mainIdent
- d69665f6|||PR ident#34 part 3: shared look classes (wide, avatar, chip/used, conn, choose, who, inboxlink) moved to statics or droppedmre
- 3a64dc48ident#34: chipLook: radius as the same var() string (a bare token name in a static does not compile); selector gate selectormre
- eb039073ident#34: tests: signin and migration find the choice without the classmre
- 4b94a594ident#34: tests: fix selector of the identity choicemre