moment#isMoment TypeScript Examples

The following examples show how to use moment#isMoment. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: expr.ts    From obsidian-tracker with MIT License 6 votes vote down vote up
function checkBinaryOperantType(left: any, right: any) {
    if (typeof left === "string") return left;
    if (typeof right === "string") return right;
    if (
        typeof left !== "number" &&
        !window.moment.isMoment(left) &&
        !(left instanceof Dataset)
    ) {
        return "Error: invalid operant type";
    }
    if (
        typeof right !== "number" &&
        !window.moment.isMoment(right) &&
        !(right instanceof Dataset)
    ) {
        return "Error: invalide operant type";
    }
    return "";
}
Example #2
Source File: expr.ts    From obsidian-tracker with MIT License 6 votes vote down vote up
// Resolve the template expression in string and return a resolved string
export function resolveTemplate(
    template: string,
    renderInfo: RenderInfo
): string {
    let retResolve = resolve(template, renderInfo);
    if (typeof retResolve === "string") {
        return retResolve; // error message
    }
    let exprMap = retResolve as Array<ExprResolved>;

    for (let exprResolved of exprMap) {
        let source = exprResolved.source;
        let value = exprResolved.value;
        let format = exprResolved.format;
        let strValue = "";
        if (typeof value === "number") {
            if (format) {
                strValue = sprintf("%" + format, value);
            } else {
                strValue = value.toFixed(1);
            }
        } else if (window.moment.isMoment(value)) {
            if (format) {
                strValue = helper.dateToStr(value, format);
            } else {
                strValue = helper.dateToStr(value, renderInfo.dateFormat);
            }
        }

        if (strValue) {
            // console.log(exprResolved);
            template = template.split(source).join(strValue);
        }
    }

    return template;
}
Example #3
Source File: ItemsTypeModify.tsx    From yforms with MIT License 5 votes vote down vote up
dateFormat = (
  value?: any,
  props?: { picker?: PickerMode; format?: any },
  type?: string,
  pureValue?: boolean,
) => {
  const { picker = 'date', format } = props;
  let _format;
  if (format) {
    _format = format;
  } else if (picker === 'date') {
    const { showTime } = props as PickerPanelDateProps<'date'>;
    let timeFormat = '';
    if (showTime) {
      timeFormat = typeof showTime === 'boolean' ? 'HH:mm:ss' : showTime.format;
    }
    _format = timeFormat ? `YYYY-MM-DD ${timeFormat}` : 'YYYY-MM-DD';
  } else if (picker === 'year') {
    _format = 'YYYY';
  } else if (picker === 'quarter') {
    _format = 'YYYY-\\QQ';
  } else if (picker === 'month') {
    _format = 'YYYY-MM';
  } else if (picker === 'week') {
    _format = 'YYYY-wo';
  } else if (picker === 'time') {
    _format = 'HH:mm:ss';
  }
  const dateFormat = typeof _format === 'string' ? _format : _format[0];

  if (type === 'datePicker') {
    if (isMoment(value)) {
      return moment(value).format(dateFormat);
    }
  } else if (type === 'rangePicker') {
    const { separator } = props as any;
    const [start, end] = value || [];
    if (pureValue) {
      return `${start && moment(start).format(dateFormat)}${end && moment(end).format(dateFormat)}`;
    }
    return (
      value && (
        <>
          {value && value[0] ? moment(value[0]).format(dateFormat) : noData}
          &nbsp;{separator || <SwapRightOutlined />}&nbsp;
          {value && value[1] ? moment(value[1]).format(dateFormat) : noData}
        </>
      )
    );
  }
}
Example #4
Source File: expr.ts    From obsidian-tracker with MIT License 5 votes vote down vote up
// Get a list of resolved result containing source, value, and format
function resolve(
    text: string,
    renderInfo: RenderInfo
): Array<ExprResolved> | string {
    // console.log(text);

    let exprMap: Array<ExprResolved> = [];

    // {{(?<expr>[\w+\-*\/0-9\s()\[\]%.]+)(::(?<format>[\w+\-*\/0-9\s()\[\]%.:]+))?}}
    let strExprRegex =
        "{{(?<expr>[\\w+\\-*\\/0-9\\s()\\[\\]%.,]+)(::(?<format>[\\w+\\-*\\/0-9\\s()\\[\\]%.:]+))?}}";
    let exprRegex = new RegExp(strExprRegex, "gm");
    let match;
    while ((match = exprRegex.exec(text))) {
        // console.log(match);
        let fullmatch = match[0];
        if (exprMap.some((e) => e.source === fullmatch)) continue;

        if (typeof match.groups !== "undefined") {
            if (typeof match.groups.expr !== "undefined") {
                let expr = match.groups.expr;

                let ast = null;
                try {
                    ast = jsep(expr);
                } catch (err) {
                    return "Error:" + err.message;
                }
                if (!ast) {
                    return "Error: failed to parse expression";
                }
                // console.log(ast);

                const value = evaluate(ast, renderInfo);
                if (typeof value === "string") {
                    return value; // error message
                }

                if (
                    typeof value === "number" ||
                    window.moment.isMoment(value)
                ) {
                    let format = null;
                    if (typeof match.groups.format !== "undefined") {
                        format = match.groups.format;
                    }

                    exprMap.push({
                        source: fullmatch,
                        value: value,
                        format: format,
                    });
                }
            }
        }
    }

    return exprMap;
}