@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
3 lines (2 loc) • 4.21 kB
JavaScript
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */
import t from"./DateTimeFieldFormat.js";import e from"./FieldFormat.js";import{isFieldWithType as n,isNumericField as r,isDateField as o,isDateOnlyField as a,isTimeOnlyField as u,isTimestampOffsetField as i}from"./fieldUtils.js";import s from"./NumberFieldFormat.js";import l from"../../popup/support/FieldInfoFormat.js";const c={base:e,key:"type",errorContext:"field-configuration-field-format",typeMap:{"date-time":t,number:s}};function m(t,e){const{format:n}=t;return e&&d(e)?n?h(n,e):N(e,{hour12:"always",useGrouping:"always"}):null}function h(t,e){const{dateFormat:n}=t;if(!e)return n?g(t):y(t);if(!d(e))return;const r={hour12:"always"};switch(e.type){case"integer":case"small-integer":case"big-integer":case"long":return f(t);case"single":case"double":return y(t);case"date":case"timestamp-offset":return n?g(t):N(e,r);case"date-only":return n?p(t):N(e);case"time-only":return n?w(t):N(e,r)}}function d(t){return n(t)&&(r(t)||o(t)||a(t)||u(t)||i(t))}function f(t){return new s({useGrouping:t.digitSeparator?"always":"never"})}function y(t){const e=f(t),{places:n}=t;return e.minimumFractionDigits=n??0,e.maximumFractionDigits=n??2,e}function g(e){return new t({dateStyle:F(e),year:D(e),month:$(e),timeStyle:b(e),hour12:S(e)})}function p(e){return new t({dateStyle:F(e),year:D(e),month:$(e)})}function w(e){return new t({timeStyle:b(e),hour12:S(e)})}function F(t){const{dateFormat:e}=t;return e?e.startsWith("short-date")?"short":e.startsWith("day-short-month-year")?"medium":e.startsWith("long-month-day-year")?"long":e.startsWith("long-date")?"full":null:null}function S(t){return t.dateFormat?.endsWith("-24")?"never":"always"}function $(t){switch(t.dateFormat){case"short-month-year":return"short";case"long-month-year":return"long";default:return null}}function b(t){const{dateFormat:e}=t;return e?e.includes("short-time")?"short":e.includes("long-time")?"medium":null:null}function D(t){switch(t.dateFormat){case"short-month-year":case"long-month-year":case"year":return"numeric";default:return null}}function x(t,e){const n="number"===t.type,r="date-time"===t.type;if(!e)return n?j(t):r?v(t):null;if(d(e))switch(e.type){case"integer":case"small-integer":case"big-integer":case"long":return n?G(t):null;case"single":case"double":return n?j(t):null;case"date":case"timestamp-offset":return r?v(t):null;case"date-only":return r?W(t):null;case"time-only":return r?k(t):null}}function G(t){return new l({digitSeparator:"never"!==t.useGrouping})}function j(t){const e=G(t);return e.places=t.maximumFractionDigits,e}function v(t){let e;const n=C(t),r=I(t),o="never"===t.hour12?"24":null,a="numeric"===t.year?"year":null,u=M(t);return n&&r&&o?e=`${n}-${r}-${o}`:n&&r?e=`${n}-${r}`:n?e=n:a&&u?e=`${u}-${a}`:a&&(e=a),e?new l({dateFormat:e}):null}function W(t){let e;const n=C(t),r="numeric"===t.year?"year":null,o=M(t);return n?e=n:r&&o?e=`${o}-${r}`:r&&(e=r),e?new l({dateFormat:e}):null}function k(t){let e;const n=I(t),r="never"===t.hour12?"24":null;return n&&r?e=`short-date-${n}-${r}`:n&&(e=`short-date-${n}`),e?new l({dateFormat:e}):null}function C(t){switch(t.dateStyle){case"short":return"short-date";case"medium":return"day-short-month-year";case"long":return"long-month-day-year";case"full":return"long-date";default:return}}function I(t){return t.timeStyle&&("short"===t.timeStyle?"short-time":"long-time")}function M(t){switch(t.month){case"short":return"short-month";case"long":return"long-month";default:return}}function N(e,n){if(!d(e))return;const r=n?.useGrouping??"auto",o=n?.hour12??"auto";switch(e.type){case"integer":case"small-integer":case"big-integer":case"long":return new s({minimumFractionDigits:0,maximumFractionDigits:0,useGrouping:r});case"single":case"double":return new s({minimumFractionDigits:0,maximumFractionDigits:2,useGrouping:r});case"date":case"timestamp-offset":return new t({dateStyle:"short",timeStyle:"short",hour12:o});case"date-only":return new t({dateStyle:"short"});case"time-only":return new t({timeStyle:"short",hour12:o})}}export{N as defaultFieldFormat,m as fieldFormatFromFieldInfo,h as fieldFormatFromFieldInfoFormat,x as fieldInfoFormatFromFieldFormat,c as formatTypes,d as isFormattableField};