Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/util/cmp.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { isEqual } from "lodash";67export function cmp(a: any, b: any): number {8if (a < b) {9return -1;10} else if (a > b) {11return 1;12}13return 0;14}1516/*17compare two Date | undefined | null objects.1819null and undefined are considered equal to each other.2021null_last:22- true: nulls are infinitely in the future23- false: nulls are the dawn of mankind24*/2526export function cmp_Date(27a: Date | undefined | null,28b: Date | undefined | null,29null_last = false30): number {31if (a == null) {32if (b == null) {33return 0;34}35return null_last ? 1 : -1;36}37// a != null38if (b == null) {39return null_last ? -1 : 1;40}41if (a < b) return -1;42if (a > b) return 1;43return 0; // note: a == b for Date objects doesn't work as expected, but that's OK here.44}4546export function cmp_moment(a?, b?, null_last = false): number {47return cmp_Date(a?.toDate(), b?.toDate(), null_last);48}4950export function cmp_dayjs(a?, b?, null_last = false): number {51return cmp_Date(a?.toDate(), b?.toDate(), null_last);52}5354export function cmp_array(a, b): number {55const end = Math.max(a.length, b.length);56for (let i = 0; i < end; i++) {57const c = cmp(a[i], b[i]);58if (c) {59return c;60}61}62return 0;63}6465export function timestamp_cmp(a, b, field): number {66if (field == null) {67field = "timestamp";68}69return -cmp_Date(a[field], b[field]);70}7172export function field_cmp(field: string | string[]): (a, b) => number {73if (typeof field == "string") {74return (a, b) => cmp(a[field], b[field]);75} else {76// array of strings77return (a, b) => {78for (const f of field) {79const c = cmp(a[f], b[f]);80if (c) return c;81}82return 0;83};84}85}8687export function all_fields_equal<T extends { [K: string]: any }>(88a: T,89b: T,90fields: (keyof T)[],91verbose?: any92) {93return !is_different(a, b, fields, verbose);94}9596export function is_different<T extends { [K: string]: any }>(97a: T,98b: T,99fields: (keyof T)[],100verbose?: any101): boolean {102if (verbose != null) {103return is_different_verbose(a, b, fields, verbose);104}105let field: keyof T;106if (a == null) {107if (b == null) {108return false; // they are the same109}110// a not defined but b is111for (field of fields) {112if (b[field] != null) {113return true;114}115}116return false;117}118if (b == null) {119// a is defined or would be handled above120for (field of fields) {121if (a[field] != null) {122return true; // different123}124}125return false; // same126}127128for (field of fields) {129if (a[field] !== b[field]) {130return true;131}132}133return false;134}135136// Use for debugging purposes only -- copy code from above to avoid making that137// code more complicated and possibly slower.138function is_different_verbose(a, b, fields, verbose): boolean {139function log(...x) {140console.log("is_different_verbose", verbose, ...x);141}142let field: string;143if (a == null) {144if (b == null) {145log("both null");146return false; // they are the same147}148// a not defined but b is149for (field of fields) {150if (b[field] != null) {151log("a not defined but b is");152return true;153}154}155return false;156}157if (b == null) {158// a is defined or would be handled above159for (field of fields) {160if (a[field] != null) {161log(`b null and "${field}" of a is not null`);162return true; // different163}164}165return false; // same166}167168for (field of fields) {169if (a[field] !== b[field]) {170log(`field "${field}" differs`, a[field], b[field]);171return true;172}173}174log("same");175return false;176}177178export const is_different_array = (a, b) => !isEqual(a, b);179180// See https://stackoverflow.com/questions/22266826/how-can-i-do-a-shallow-comparison-of-the-properties-of-two-objects-with-javascri/22266891#22266891181export const shallowCompare = (obj1, obj2) =>182Object.keys(obj1).length === Object.keys(obj2).length &&183Object.keys(obj1).every(184(key) => obj2.hasOwnProperty(key) && obj1[key] === obj2[key]185);186187188