@sanity/util
Advanced tools
Comparing version 3.34.1-cds-unstable.22 to 3.35.0
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
//# sourceMappingURL=index.js.map |
@@ -1,3 +0,3 @@ | ||
import { from, switchMap, finalize } from 'rxjs'; | ||
import { ConcurrencyLimiter } from './concurrency-limiter.esm.js'; | ||
import { from, switchMap, finalize } from "rxjs"; | ||
import { ConcurrencyLimiter } from "./concurrency-limiter.esm.js"; | ||
function createClientConcurrencyLimiter(maxConcurrency) { | ||
@@ -10,40 +10,23 @@ const limiter = new ConcurrencyLimiter(maxConcurrency); | ||
case "fetch": | ||
{ | ||
return async function () { | ||
await limiter.ready(); | ||
try { | ||
return await target.fetch(...arguments); | ||
} finally { | ||
limiter.release(); | ||
} | ||
}; | ||
} | ||
return async (...args) => { | ||
await limiter.ready(); | ||
try { | ||
return await target.fetch(...args); | ||
} finally { | ||
limiter.release(); | ||
} | ||
}; | ||
case "clone": | ||
{ | ||
return function () { | ||
return wrapClient(target.clone(...arguments)); | ||
}; | ||
} | ||
return (...args) => wrapClient(target.clone(...args)); | ||
case "config": | ||
{ | ||
return function () { | ||
const result = target.config(...arguments); | ||
if (arguments.length <= 0 ? undefined : arguments[0]) return wrapClient(result); | ||
return result; | ||
}; | ||
} | ||
return (...args) => { | ||
const result = target.config(...args); | ||
return args[0] ? wrapClient(result) : result; | ||
}; | ||
case "withConfig": | ||
{ | ||
return function () { | ||
return wrapClient(target.withConfig(...arguments)); | ||
}; | ||
} | ||
return (...args) => wrapClient(target.withConfig(...args)); | ||
case "observable": | ||
{ | ||
return wrapObservableClient(target.observable); | ||
} | ||
return wrapObservableClient(target.observable); | ||
default: | ||
{ | ||
return target[property]; | ||
} | ||
return target[property]; | ||
} | ||
@@ -58,34 +41,17 @@ } | ||
case "fetch": | ||
{ | ||
return function () { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return from(limiter.ready()).pipe(switchMap(() => target.fetch(...args)), finalize(() => limiter.release())); | ||
}; | ||
} | ||
return (...args) => from(limiter.ready()).pipe( | ||
switchMap(() => target.fetch(...args)), | ||
finalize(() => limiter.release()) | ||
); | ||
case "clone": | ||
{ | ||
return function () { | ||
return wrapObservableClient(target.clone(...arguments)); | ||
}; | ||
} | ||
return (...args) => wrapObservableClient(target.clone(...args)); | ||
case "config": | ||
{ | ||
return function () { | ||
const result = target.config(...arguments); | ||
if (arguments.length <= 0 ? undefined : arguments[0]) return wrapObservableClient(result); | ||
return result; | ||
}; | ||
} | ||
return (...args) => { | ||
const result = target.config(...args); | ||
return args[0] ? wrapObservableClient(result) : result; | ||
}; | ||
case "withConfig": | ||
{ | ||
return function () { | ||
return wrapObservableClient(target.withConfig(...arguments)); | ||
}; | ||
} | ||
return (...args) => wrapObservableClient(target.withConfig(...args)); | ||
default: | ||
{ | ||
return target[property]; | ||
} | ||
return target[property]; | ||
} | ||
@@ -97,3 +63,5 @@ } | ||
} | ||
export { createClientConcurrencyLimiter }; | ||
export { | ||
createClientConcurrencyLimiter | ||
}; | ||
//# sourceMappingURL=client.esm.js.map |
@@ -1,8 +0,4 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
var rxjs = require('rxjs'); | ||
var concurrencyLimiter = require('./concurrency-limiter.js'); | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: !0 }); | ||
var rxjs = require("rxjs"), concurrencyLimiter = require("./concurrency-limiter.js"); | ||
function createClientConcurrencyLimiter(maxConcurrency) { | ||
@@ -15,40 +11,23 @@ const limiter = new concurrencyLimiter.ConcurrencyLimiter(maxConcurrency); | ||
case "fetch": | ||
{ | ||
return async function () { | ||
await limiter.ready(); | ||
try { | ||
return await target.fetch(...arguments); | ||
} finally { | ||
limiter.release(); | ||
} | ||
}; | ||
} | ||
return async (...args) => { | ||
await limiter.ready(); | ||
try { | ||
return await target.fetch(...args); | ||
} finally { | ||
limiter.release(); | ||
} | ||
}; | ||
case "clone": | ||
{ | ||
return function () { | ||
return wrapClient(target.clone(...arguments)); | ||
}; | ||
} | ||
return (...args) => wrapClient(target.clone(...args)); | ||
case "config": | ||
{ | ||
return function () { | ||
const result = target.config(...arguments); | ||
if (arguments.length <= 0 ? undefined : arguments[0]) return wrapClient(result); | ||
return result; | ||
}; | ||
} | ||
return (...args) => { | ||
const result = target.config(...args); | ||
return args[0] ? wrapClient(result) : result; | ||
}; | ||
case "withConfig": | ||
{ | ||
return function () { | ||
return wrapClient(target.withConfig(...arguments)); | ||
}; | ||
} | ||
return (...args) => wrapClient(target.withConfig(...args)); | ||
case "observable": | ||
{ | ||
return wrapObservableClient(target.observable); | ||
} | ||
return wrapObservableClient(target.observable); | ||
default: | ||
{ | ||
return target[property]; | ||
} | ||
return target[property]; | ||
} | ||
@@ -63,34 +42,17 @@ } | ||
case "fetch": | ||
{ | ||
return function () { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return rxjs.from(limiter.ready()).pipe(rxjs.switchMap(() => target.fetch(...args)), rxjs.finalize(() => limiter.release())); | ||
}; | ||
} | ||
return (...args) => rxjs.from(limiter.ready()).pipe( | ||
rxjs.switchMap(() => target.fetch(...args)), | ||
rxjs.finalize(() => limiter.release()) | ||
); | ||
case "clone": | ||
{ | ||
return function () { | ||
return wrapObservableClient(target.clone(...arguments)); | ||
}; | ||
} | ||
return (...args) => wrapObservableClient(target.clone(...args)); | ||
case "config": | ||
{ | ||
return function () { | ||
const result = target.config(...arguments); | ||
if (arguments.length <= 0 ? undefined : arguments[0]) return wrapObservableClient(result); | ||
return result; | ||
}; | ||
} | ||
return (...args) => { | ||
const result = target.config(...args); | ||
return args[0] ? wrapObservableClient(result) : result; | ||
}; | ||
case "withConfig": | ||
{ | ||
return function () { | ||
return wrapObservableClient(target.withConfig(...arguments)); | ||
}; | ||
} | ||
return (...args) => wrapObservableClient(target.withConfig(...args)); | ||
default: | ||
{ | ||
return target[property]; | ||
} | ||
return target[property]; | ||
} | ||
@@ -97,0 +59,0 @@ } |
class ConcurrencyLimiter { | ||
constructor(max) { | ||
this.max = max; | ||
this.current = 0; | ||
this.resolvers = []; | ||
/** | ||
* Indicates when a slot for a new operation is ready. | ||
* If under the limit, it resolves immediately; otherwise, it waits until a slot is free. | ||
*/ | ||
this.ready = () => { | ||
if (this.max === Infinity) return Promise.resolve(); | ||
if (this.current < this.max) { | ||
this.current++; | ||
return Promise.resolve(); | ||
} | ||
return new Promise(resolve => { | ||
this.resolvers.push(resolve); | ||
}); | ||
}; | ||
/** | ||
* Releases a slot, decrementing the current count of operations if nothing is in the queue. | ||
* If there are operations waiting, it allows the next one in the queue to proceed. | ||
*/ | ||
this.release = () => { | ||
if (this.max === Infinity) return; | ||
this.max = max, this.current = 0, this.resolvers = [], this.ready = () => this.max === 1 / 0 ? Promise.resolve() : this.current < this.max ? (this.current++, Promise.resolve()) : new Promise((resolve) => { | ||
this.resolvers.push(resolve); | ||
}), this.release = () => { | ||
if (this.max === 1 / 0) | ||
return; | ||
const nextResolver = this.resolvers.shift(); | ||
@@ -35,3 +17,5 @@ if (nextResolver) { | ||
} | ||
export { ConcurrencyLimiter }; | ||
export { | ||
ConcurrencyLimiter | ||
}; | ||
//# sourceMappingURL=concurrency-limiter.esm.js.map |
@@ -1,31 +0,10 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: !0 }); | ||
class ConcurrencyLimiter { | ||
constructor(max) { | ||
this.max = max; | ||
this.current = 0; | ||
this.resolvers = []; | ||
/** | ||
* Indicates when a slot for a new operation is ready. | ||
* If under the limit, it resolves immediately; otherwise, it waits until a slot is free. | ||
*/ | ||
this.ready = () => { | ||
if (this.max === Infinity) return Promise.resolve(); | ||
if (this.current < this.max) { | ||
this.current++; | ||
return Promise.resolve(); | ||
} | ||
return new Promise(resolve => { | ||
this.resolvers.push(resolve); | ||
}); | ||
}; | ||
/** | ||
* Releases a slot, decrementing the current count of operations if nothing is in the queue. | ||
* If there are operations waiting, it allows the next one in the queue to proceed. | ||
*/ | ||
this.release = () => { | ||
if (this.max === Infinity) return; | ||
this.max = max, this.current = 0, this.resolvers = [], this.ready = () => this.max === 1 / 0 ? Promise.resolve() : this.current < this.max ? (this.current++, Promise.resolve()) : new Promise((resolve) => { | ||
this.resolvers.push(resolve); | ||
}), this.release = () => { | ||
if (this.max === 1 / 0) | ||
return; | ||
const nextResolver = this.resolvers.shift(); | ||
@@ -32,0 +11,0 @@ if (nextResolver) { |
@@ -1,67 +0,42 @@ | ||
import getRandomValues from 'get-random-values-esm'; | ||
import getRandomValues from "get-random-values-esm"; | ||
var hasOwn = Object.prototype.hasOwnProperty.call.bind(Object.prototype.hasOwnProperty); | ||
function isDeepEmptyObject(value) { | ||
for (const key in value) { | ||
if (key === "_type" || key === "_key") { | ||
continue; | ||
} | ||
if (hasOwn(value, key) && !isDeepEmpty(value[key])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
for (const key in value) | ||
if (!(key === "_type" || key === "_key") && hasOwn(value, key) && !isDeepEmpty(value[key])) | ||
return !1; | ||
return !0; | ||
} | ||
function isDeepEmptyArray(value) { | ||
for (let i = 0; i < value.length; i++) { | ||
if (!isDeepEmpty(value[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
for (let i = 0; i < value.length; i++) | ||
if (!isDeepEmpty(value[i])) | ||
return !1; | ||
return !0; | ||
} | ||
function isDeepEmpty(value) { | ||
if (value === void 0 || value === null) { | ||
return true; | ||
} | ||
if (value == null) | ||
return !0; | ||
const type = typeof value; | ||
if (Array.isArray(value)) { | ||
return isDeepEmptyArray(value); | ||
} | ||
if (type === "object") { | ||
return isDeepEmptyObject(value); | ||
} | ||
return false; | ||
return Array.isArray(value) ? isDeepEmptyArray(value) : type === "object" ? isDeepEmptyObject(value) : !1; | ||
} | ||
const isEmptyArray = isDeepEmptyArray; | ||
const isEmpty = isDeepEmpty; | ||
const isEmptyObject = isDeepEmptyObject; | ||
const isEmptyArray = isDeepEmptyArray, isEmpty = isDeepEmpty, isEmptyObject = isDeepEmptyObject; | ||
function isShallowEmptyObject(value) { | ||
for (const key in value) { | ||
if (key === "_type" || key === "_key") { | ||
continue; | ||
} | ||
if (hasOwn(value, key) && value[key] !== void 0) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
for (const key in value) | ||
if (!(key === "_type" || key === "_key") && hasOwn(value, key) && value[key] !== void 0) | ||
return !1; | ||
return !0; | ||
} | ||
const getByteHexTable = /* @__PURE__ */(() => { | ||
const getByteHexTable = /* @__PURE__ */ (() => { | ||
let table; | ||
return () => { | ||
if (table) { | ||
if (table) | ||
return table; | ||
} | ||
table = []; | ||
for (let i = 0; i < 256; ++i) { | ||
for (let i = 0; i < 256; ++i) | ||
table[i] = (i + 256).toString(16).substring(1); | ||
} | ||
return table; | ||
}; | ||
})(); | ||
function whatwgRNG() { | ||
let length = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 16; | ||
function whatwgRNG(length = 16) { | ||
const rnds8 = new Uint8Array(length); | ||
getRandomValues(rnds8); | ||
return rnds8; | ||
return getRandomValues(rnds8), rnds8; | ||
} | ||
@@ -88,36 +63,28 @@ function randomKey(length) { | ||
} | ||
if (typeof val == "object" && val && typeof val.length == "number") { | ||
if (typeof val == "object" && val && typeof val.length == "number") | ||
try { | ||
if (typeof val.callee == "function") { | ||
if (typeof val.callee == "function") | ||
return "arguments"; | ||
} | ||
} catch (ex) { | ||
if (ex instanceof TypeError) { | ||
if (ex instanceof TypeError) | ||
return "arguments"; | ||
} | ||
} | ||
} | ||
if (val === null) { | ||
return "null"; | ||
} | ||
if (val === void 0) { | ||
return "undefined"; | ||
} | ||
if (val && val.nodeType === 1) { | ||
return "element"; | ||
} | ||
if (val === Object(val)) { | ||
return "object"; | ||
} | ||
return typeof val; | ||
return val === null ? "null" : val === void 0 ? "undefined" : val && val.nodeType === 1 ? "element" : val === Object(val) ? "object" : typeof val; | ||
} | ||
function resolveTypeName(value) { | ||
const jsType = resolveJSType(value); | ||
if (jsType !== "object") { | ||
if (jsType !== "object") | ||
return jsType; | ||
} | ||
const obj = value; | ||
return "_type" in obj && obj._type || jsType; | ||
} | ||
export { isDeepEmpty, isEmpty, isEmptyArray, isEmptyObject, isShallowEmptyObject, randomKey, resolveTypeName }; | ||
export { | ||
isDeepEmpty, | ||
isEmpty, | ||
isEmptyArray, | ||
isEmptyObject, | ||
isShallowEmptyObject, | ||
randomKey, | ||
resolveTypeName | ||
}; | ||
//# sourceMappingURL=content.esm.js.map |
@@ -1,78 +0,47 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
var getRandomValues = require('get-random-values-esm'); | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: !0 }); | ||
var getRandomValues = require("get-random-values-esm"); | ||
function _interopDefaultCompat(e) { | ||
return e && typeof e === 'object' && 'default' in e ? e : { | ||
default: e | ||
}; | ||
return e && typeof e == "object" && "default" in e ? e : { default: e }; | ||
} | ||
var getRandomValues__default = /*#__PURE__*/_interopDefaultCompat(getRandomValues); | ||
var hasOwn = Object.prototype.hasOwnProperty.call.bind(Object.prototype.hasOwnProperty); | ||
var getRandomValues__default = /* @__PURE__ */ _interopDefaultCompat(getRandomValues), hasOwn = Object.prototype.hasOwnProperty.call.bind(Object.prototype.hasOwnProperty); | ||
function isDeepEmptyObject(value) { | ||
for (const key in value) { | ||
if (key === "_type" || key === "_key") { | ||
continue; | ||
} | ||
if (hasOwn(value, key) && !isDeepEmpty(value[key])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
for (const key in value) | ||
if (!(key === "_type" || key === "_key") && hasOwn(value, key) && !isDeepEmpty(value[key])) | ||
return !1; | ||
return !0; | ||
} | ||
function isDeepEmptyArray(value) { | ||
for (let i = 0; i < value.length; i++) { | ||
if (!isDeepEmpty(value[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
for (let i = 0; i < value.length; i++) | ||
if (!isDeepEmpty(value[i])) | ||
return !1; | ||
return !0; | ||
} | ||
function isDeepEmpty(value) { | ||
if (value === void 0 || value === null) { | ||
return true; | ||
} | ||
if (value == null) | ||
return !0; | ||
const type = typeof value; | ||
if (Array.isArray(value)) { | ||
return isDeepEmptyArray(value); | ||
} | ||
if (type === "object") { | ||
return isDeepEmptyObject(value); | ||
} | ||
return false; | ||
return Array.isArray(value) ? isDeepEmptyArray(value) : type === "object" ? isDeepEmptyObject(value) : !1; | ||
} | ||
const isEmptyArray = isDeepEmptyArray; | ||
const isEmpty = isDeepEmpty; | ||
const isEmptyObject = isDeepEmptyObject; | ||
const isEmptyArray = isDeepEmptyArray, isEmpty = isDeepEmpty, isEmptyObject = isDeepEmptyObject; | ||
function isShallowEmptyObject(value) { | ||
for (const key in value) { | ||
if (key === "_type" || key === "_key") { | ||
continue; | ||
} | ||
if (hasOwn(value, key) && value[key] !== void 0) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
for (const key in value) | ||
if (!(key === "_type" || key === "_key") && hasOwn(value, key) && value[key] !== void 0) | ||
return !1; | ||
return !0; | ||
} | ||
const getByteHexTable = /* @__PURE__ */(() => { | ||
const getByteHexTable = /* @__PURE__ */ (() => { | ||
let table; | ||
return () => { | ||
if (table) { | ||
if (table) | ||
return table; | ||
} | ||
table = []; | ||
for (let i = 0; i < 256; ++i) { | ||
for (let i = 0; i < 256; ++i) | ||
table[i] = (i + 256).toString(16).substring(1); | ||
} | ||
return table; | ||
}; | ||
})(); | ||
function whatwgRNG() { | ||
let length = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 16; | ||
function whatwgRNG(length = 16) { | ||
const rnds8 = new Uint8Array(length); | ||
getRandomValues__default.default(rnds8); | ||
return rnds8; | ||
return getRandomValues__default.default(rnds8), rnds8; | ||
} | ||
@@ -99,32 +68,16 @@ function randomKey(length) { | ||
} | ||
if (typeof val == "object" && val && typeof val.length == "number") { | ||
if (typeof val == "object" && val && typeof val.length == "number") | ||
try { | ||
if (typeof val.callee == "function") { | ||
if (typeof val.callee == "function") | ||
return "arguments"; | ||
} | ||
} catch (ex) { | ||
if (ex instanceof TypeError) { | ||
if (ex instanceof TypeError) | ||
return "arguments"; | ||
} | ||
} | ||
} | ||
if (val === null) { | ||
return "null"; | ||
} | ||
if (val === void 0) { | ||
return "undefined"; | ||
} | ||
if (val && val.nodeType === 1) { | ||
return "element"; | ||
} | ||
if (val === Object(val)) { | ||
return "object"; | ||
} | ||
return typeof val; | ||
return val === null ? "null" : val === void 0 ? "undefined" : val && val.nodeType === 1 ? "element" : val === Object(val) ? "object" : typeof val; | ||
} | ||
function resolveTypeName(value) { | ||
const jsType = resolveJSType(value); | ||
if (jsType !== "object") { | ||
if (jsType !== "object") | ||
return jsType; | ||
} | ||
const obj = value; | ||
@@ -131,0 +84,0 @@ return "_type" in obj && obj._type || jsType; |
@@ -1,6 +0,3 @@ | ||
function createSafeJsonParser(_ref) { | ||
let { | ||
errorLabel | ||
} = _ref; | ||
return function safeJsonParser(line) { | ||
function createSafeJsonParser({ errorLabel }) { | ||
return function(line) { | ||
try { | ||
@@ -10,17 +7,15 @@ return JSON.parse(line); | ||
const errorPosition = line.lastIndexOf('{"error":'); | ||
if (errorPosition === -1) { | ||
err.message = "".concat(err.message, " (").concat(line, ")"); | ||
throw err; | ||
} | ||
const errorJson = line.slice(errorPosition); | ||
const errorLine = JSON.parse(errorJson); | ||
const error = errorLine && errorLine.error; | ||
if (error && error.description) { | ||
throw new Error("".concat(errorLabel, ": ").concat(error.description, "\n\n").concat(errorJson, "\n")); | ||
} | ||
throw err; | ||
if (errorPosition === -1) | ||
throw err.message = `${err.message} (${line})`, err; | ||
const errorJson = line.slice(errorPosition), errorLine = JSON.parse(errorJson), error = errorLine && errorLine.error; | ||
throw error && error.description ? new Error(`${errorLabel}: ${error.description} | ||
${errorJson} | ||
`) : err; | ||
} | ||
}; | ||
} | ||
export { createSafeJsonParser }; | ||
export { | ||
createSafeJsonParser | ||
}; | ||
//# sourceMappingURL=createSafeJsonParser.esm.js.map |
@@ -1,11 +0,5 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
function createSafeJsonParser(_ref) { | ||
let { | ||
errorLabel | ||
} = _ref; | ||
return function safeJsonParser(line) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: !0 }); | ||
function createSafeJsonParser({ errorLabel }) { | ||
return function(line) { | ||
try { | ||
@@ -15,13 +9,9 @@ return JSON.parse(line); | ||
const errorPosition = line.lastIndexOf('{"error":'); | ||
if (errorPosition === -1) { | ||
err.message = "".concat(err.message, " (").concat(line, ")"); | ||
throw err; | ||
} | ||
const errorJson = line.slice(errorPosition); | ||
const errorLine = JSON.parse(errorJson); | ||
const error = errorLine && errorLine.error; | ||
if (error && error.description) { | ||
throw new Error("".concat(errorLabel, ": ").concat(error.description, "\n\n").concat(errorJson, "\n")); | ||
} | ||
throw err; | ||
if (errorPosition === -1) | ||
throw err.message = `${err.message} (${line})`, err; | ||
const errorJson = line.slice(errorPosition), errorLine = JSON.parse(errorJson), error = errorLine && errorLine.error; | ||
throw error && error.description ? new Error(`${errorLabel}: ${error.description} | ||
${errorJson} | ||
`) : err; | ||
} | ||
@@ -28,0 +18,0 @@ }; |
@@ -1,12 +0,10 @@ | ||
import fs from 'fs/promises'; | ||
import os from 'os'; | ||
import path from 'path'; | ||
import fs from "fs/promises"; | ||
import os from "os"; | ||
import path from "path"; | ||
async function pathIsEmpty(dir) { | ||
try { | ||
const content = await fs.readdir(absolutify(dir)); | ||
return content.length === 0; | ||
return (await fs.readdir(absolutify(dir))).length === 0; | ||
} catch (err) { | ||
if (err.code === "ENOENT") { | ||
return true; | ||
} | ||
if (err.code === "ENOENT") | ||
return !0; | ||
throw err; | ||
@@ -17,5 +15,4 @@ } | ||
if (filePath.charCodeAt(0) === 126) { | ||
if (filePath.charCodeAt(1) === 43) { | ||
if (filePath.charCodeAt(1) === 43) | ||
return path.join(process.cwd(), filePath.slice(2)); | ||
} | ||
const home = os.homedir(); | ||
@@ -30,3 +27,7 @@ return home ? path.join(home, filePath.slice(1)) : filePath; | ||
} | ||
export { absolutify, expandHome, pathIsEmpty }; | ||
export { | ||
absolutify, | ||
expandHome, | ||
pathIsEmpty | ||
}; | ||
//# sourceMappingURL=fs.esm.js.map |
@@ -1,25 +0,14 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
var fs = require('fs/promises'); | ||
var os = require('os'); | ||
var path = require('path'); | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: !0 }); | ||
var fs = require("fs/promises"), os = require("os"), path = require("path"); | ||
function _interopDefaultCompat(e) { | ||
return e && typeof e === 'object' && 'default' in e ? e : { | ||
default: e | ||
}; | ||
return e && typeof e == "object" && "default" in e ? e : { default: e }; | ||
} | ||
var fs__default = /*#__PURE__*/_interopDefaultCompat(fs); | ||
var os__default = /*#__PURE__*/_interopDefaultCompat(os); | ||
var path__default = /*#__PURE__*/_interopDefaultCompat(path); | ||
var fs__default = /* @__PURE__ */ _interopDefaultCompat(fs), os__default = /* @__PURE__ */ _interopDefaultCompat(os), path__default = /* @__PURE__ */ _interopDefaultCompat(path); | ||
async function pathIsEmpty(dir) { | ||
try { | ||
const content = await fs__default.default.readdir(absolutify(dir)); | ||
return content.length === 0; | ||
return (await fs__default.default.readdir(absolutify(dir))).length === 0; | ||
} catch (err) { | ||
if (err.code === "ENOENT") { | ||
return true; | ||
} | ||
if (err.code === "ENOENT") | ||
return !0; | ||
throw err; | ||
@@ -30,5 +19,4 @@ } | ||
if (filePath.charCodeAt(0) === 126) { | ||
if (filePath.charCodeAt(1) === 43) { | ||
if (filePath.charCodeAt(1) === 43) | ||
return path__default.default.join(process.cwd(), filePath.slice(2)); | ||
} | ||
const home = os__default.default.homedir(); | ||
@@ -35,0 +23,0 @@ return home ? path__default.default.join(home, filePath.slice(1)) : filePath; |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
//# sourceMappingURL=index.js.map |
@@ -1,23 +0,16 @@ | ||
import moment from 'moment'; | ||
const DEFAULT_DATE_FORMAT = "YYYY-MM-DD"; | ||
const DEFAULT_TIME_FORMAT = "HH:mm"; | ||
function format(input, format2) { | ||
let useUTC = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
if (useUTC) return moment.utc(input).format(format2); | ||
return moment(input).format(format2); | ||
import moment from "moment"; | ||
const DEFAULT_DATE_FORMAT = "YYYY-MM-DD", DEFAULT_TIME_FORMAT = "HH:mm"; | ||
function format(input, format2, useUTC = !1) { | ||
return useUTC ? moment.utc(input).format(format2) : moment(input).format(format2); | ||
} | ||
function parse(dateString, format2) { | ||
const parsed = moment(dateString, format2, true); | ||
if (parsed.isValid()) { | ||
return { | ||
isValid: true, | ||
date: parsed.toDate() | ||
}; | ||
} | ||
return { | ||
isValid: false, | ||
error: 'Invalid date. Must be on the format "'.concat(format2, '"') | ||
}; | ||
const parsed = moment(dateString, format2, !0); | ||
return parsed.isValid() ? { isValid: !0, date: parsed.toDate() } : { isValid: !1, error: `Invalid date. Must be on the format "${format2}"` }; | ||
} | ||
export { DEFAULT_DATE_FORMAT, DEFAULT_TIME_FORMAT, format, parse }; | ||
export { | ||
DEFAULT_DATE_FORMAT, | ||
DEFAULT_TIME_FORMAT, | ||
format, | ||
parse | ||
}; | ||
//# sourceMappingURL=legacyDateFormat.esm.js.map |
@@ -1,32 +0,15 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
var moment = require('moment'); | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: !0 }); | ||
var moment = require("moment"); | ||
function _interopDefaultCompat(e) { | ||
return e && typeof e === 'object' && 'default' in e ? e : { | ||
default: e | ||
}; | ||
return e && typeof e == "object" && "default" in e ? e : { default: e }; | ||
} | ||
var moment__default = /*#__PURE__*/_interopDefaultCompat(moment); | ||
const DEFAULT_DATE_FORMAT = "YYYY-MM-DD"; | ||
const DEFAULT_TIME_FORMAT = "HH:mm"; | ||
function format(input, format2) { | ||
let useUTC = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
if (useUTC) return moment__default.default.utc(input).format(format2); | ||
return moment__default.default(input).format(format2); | ||
var moment__default = /* @__PURE__ */ _interopDefaultCompat(moment); | ||
const DEFAULT_DATE_FORMAT = "YYYY-MM-DD", DEFAULT_TIME_FORMAT = "HH:mm"; | ||
function format(input, format2, useUTC = !1) { | ||
return useUTC ? moment__default.default.utc(input).format(format2) : moment__default.default(input).format(format2); | ||
} | ||
function parse(dateString, format2) { | ||
const parsed = moment__default.default(dateString, format2, true); | ||
if (parsed.isValid()) { | ||
return { | ||
isValid: true, | ||
date: parsed.toDate() | ||
}; | ||
} | ||
return { | ||
isValid: false, | ||
error: 'Invalid date. Must be on the format "'.concat(format2, '"') | ||
}; | ||
const parsed = moment__default.default(dateString, format2, !0); | ||
return parsed.isValid() ? { isValid: !0, date: parsed.toDate() } : { isValid: !1, error: `Invalid date. Must be on the format "${format2}"` }; | ||
} | ||
@@ -33,0 +16,0 @@ exports.DEFAULT_DATE_FORMAT = DEFAULT_DATE_FORMAT; |
@@ -1,11 +0,7 @@ | ||
import { isIndexSegment, isKeySegment, isIndexTuple } from '@sanity/types'; | ||
const rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g; | ||
const reKeySegment = /_key\s*==\s*['"](.*)['"]/; | ||
const EMPTY_PATH = []; | ||
const FOCUS_TERMINATOR = "$"; | ||
import { isIndexSegment, isKeySegment, isIndexTuple } from "@sanity/types"; | ||
const rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g, reKeySegment = /_key\s*==\s*['"](.*)['"]/, EMPTY_PATH = [], FOCUS_TERMINATOR = "$", GROQ_DATA_TYPE_VALUES = ["true", "false", "null"]; | ||
function get(obj, path, defaultVal) { | ||
const select = typeof path === "string" ? fromString(path) : path; | ||
if (!Array.isArray(select)) { | ||
const select = typeof path == "string" ? fromString(path) : path; | ||
if (!Array.isArray(select)) | ||
throw new Error("Path must be an array or a string"); | ||
} | ||
let acc = obj; | ||
@@ -15,34 +11,22 @@ for (let i = 0; i < select.length; i++) { | ||
if (isIndexSegment(segment)) { | ||
if (!Array.isArray(acc)) { | ||
if (!Array.isArray(acc)) | ||
return defaultVal; | ||
} | ||
acc = acc[segment]; | ||
} | ||
if (isKeySegment(segment)) { | ||
if (!Array.isArray(acc)) { | ||
if (!Array.isArray(acc)) | ||
return defaultVal; | ||
} | ||
acc = acc.find(item => item._key === segment._key); | ||
acc = acc.find((item) => item._key === segment._key); | ||
} | ||
if (typeof segment === "string") { | ||
acc = typeof acc === "object" && acc !== null ? acc[segment] : void 0; | ||
} | ||
if (typeof acc === "undefined") { | ||
if (typeof segment == "string" && (acc = typeof acc == "object" && acc !== null ? acc[segment] : void 0), typeof acc > "u") | ||
return defaultVal; | ||
} | ||
} | ||
return acc; | ||
} | ||
const pathsMemo = /* @__PURE__ */new Map(); | ||
const pathsMemo = /* @__PURE__ */ new Map(); | ||
function pathFor(path) { | ||
if (path.length === 0) { | ||
if (path.length === 0) | ||
return EMPTY_PATH; | ||
} | ||
const asString = toString(path); | ||
if (pathsMemo.has(asString)) { | ||
return pathsMemo.get(asString); | ||
} | ||
pathsMemo.set(asString, path); | ||
Object.freeze(path); | ||
return path; | ||
return pathsMemo.has(asString) ? pathsMemo.get(asString) : (pathsMemo.set(asString, path), Object.freeze(path), path); | ||
} | ||
@@ -54,20 +38,9 @@ function isEqual(path, otherPath) { | ||
const length = Math.min(path.length, otherPath.length); | ||
for (let i = 0; i < length; i++) { | ||
if (!isSegmentEqual(path[i], otherPath[i])) { | ||
for (let i = 0; i < length; i++) | ||
if (!isSegmentEqual(path[i], otherPath[i])) | ||
return i; | ||
} | ||
} | ||
return length; | ||
} | ||
function isSegmentEqual(segmentA, segmentB) { | ||
if (isKeySegment(segmentA) && isKeySegment(segmentB)) { | ||
return segmentA._key === segmentB._key; | ||
} | ||
if (isIndexSegment(segmentA)) { | ||
return Number(segmentA) === Number(segmentB); | ||
} | ||
if (isIndexTuple(segmentA) && isIndexTuple(segmentB)) { | ||
return segmentA[0] === segmentB[0] && segmentA[1] === segmentB[1]; | ||
} | ||
return segmentA === segmentB; | ||
return isKeySegment(segmentA) && isKeySegment(segmentB) ? segmentA._key === segmentB._key : isIndexSegment(segmentA) ? Number(segmentA) === Number(segmentB) : isIndexTuple(segmentA) && isIndexTuple(segmentB) ? segmentA[0] === segmentB[0] && segmentA[1] === segmentB[1] : segmentA === segmentB; | ||
} | ||
@@ -89,22 +62,14 @@ function hasFocus(focusPath, path) { | ||
function trimLeft(prefix, path) { | ||
if (prefix.length === 0 || path.length === 0) { | ||
if (prefix.length === 0 || path.length === 0) | ||
return path; | ||
} | ||
const [prefixHead, ...prefixTail] = prefix; | ||
const [pathHead, ...pathTail] = path; | ||
if (!isSegmentEqual(prefixHead, pathHead)) { | ||
return path; | ||
} | ||
return pathFor(trimLeft(prefixTail, pathTail)); | ||
const [prefixHead, ...prefixTail] = prefix, [pathHead, ...pathTail] = path; | ||
return isSegmentEqual(prefixHead, pathHead) ? pathFor(trimLeft(prefixTail, pathTail)) : path; | ||
} | ||
function trimRight(suffix, path) { | ||
const sufLen = suffix.length; | ||
const pathLen = path.length; | ||
if (sufLen === 0 || pathLen === 0) { | ||
const sufLen = suffix.length, pathLen = path.length; | ||
if (sufLen === 0 || pathLen === 0) | ||
return path; | ||
} | ||
let i = 0; | ||
while (i < sufLen && i < pathLen && isSegmentEqual(path[pathLen - i - 1], suffix[sufLen - i - 1])) { | ||
for (; i < sufLen && i < pathLen && isSegmentEqual(path[pathLen - i - 1], suffix[sufLen - i - 1]); ) | ||
i++; | ||
} | ||
return pathFor(path.slice(0, pathLen - i)); | ||
@@ -116,38 +81,28 @@ } | ||
function toString(path) { | ||
if (!Array.isArray(path)) { | ||
if (!Array.isArray(path)) | ||
throw new Error("Path is not an array"); | ||
} | ||
return path.reduce((target, segment, i) => { | ||
const segmentType = typeof segment; | ||
if (segmentType === "number") { | ||
return "".concat(target, "[").concat(segment, "]"); | ||
} | ||
if (segmentType === "string") { | ||
const separator = i === 0 ? "" : "."; | ||
return "".concat(target).concat(separator).concat(segment); | ||
} | ||
if (isKeySegment(segment) && segment._key) { | ||
return "".concat(target, '[_key=="').concat(segment._key, '"]'); | ||
} | ||
const isHead = i === 0; | ||
if (typeof segment == "number") | ||
return `${target}[${segment}]`; | ||
if (typeof segment == "string") | ||
return isHead ? segment : GROQ_DATA_TYPE_VALUES.includes(segment) ? `${target}["${segment}"]` : `${target}.${segment}`; | ||
if (isKeySegment(segment) && segment._key) | ||
return `${target}[_key=="${segment._key}"]`; | ||
if (Array.isArray(segment)) { | ||
const [from, to] = segment; | ||
return "".concat(target, "[").concat(from, ":").concat(to, "]"); | ||
return `${target}[${from}:${to}]`; | ||
} | ||
throw new Error("Unsupported path segment `".concat(JSON.stringify(segment), "`")); | ||
throw new Error(`Unsupported path segment \`${JSON.stringify(segment)}\``); | ||
}, ""); | ||
} | ||
function _resolveKeyedPath(value, path) { | ||
if (path.length === 0) { | ||
if (path.length === 0) | ||
return path; | ||
} | ||
const [next, ...rest] = path; | ||
if (typeof next === "number") { | ||
if (!Array.isArray(value) || !(next in value)) { | ||
if (typeof next == "number") { | ||
if (!Array.isArray(value) || !(next in value)) | ||
return []; | ||
} | ||
const item = value[next]; | ||
const key = item == null ? void 0 : item._key; | ||
return [typeof key === "string" ? { | ||
_key: item._key | ||
} : next, ..._resolveKeyedPath(item, rest)]; | ||
return [typeof (item == null ? void 0 : item._key) == "string" ? { _key: item._key } : next, ..._resolveKeyedPath(item, rest)]; | ||
} | ||
@@ -158,28 +113,16 @@ const nextVal = get(value, [next]); | ||
function resolveKeyedPath(value, path) { | ||
if (!Array.isArray(path)) { | ||
if (!Array.isArray(path)) | ||
throw new Error("Path is not an array"); | ||
} | ||
return pathFor(_resolveKeyedPath(value, path)); | ||
} | ||
function fromString(path) { | ||
if (typeof path !== "string") { | ||
if (typeof path != "string") | ||
throw new Error("Path is not a string"); | ||
} | ||
const segments = path.match(rePropName); | ||
if (!segments) { | ||
if (!segments) | ||
throw new Error("Invalid path string"); | ||
} | ||
return segments.map(normalizePathSegment); | ||
} | ||
function normalizePathSegment(segment) { | ||
if (isIndexSegment(segment)) { | ||
return normalizeIndexSegment(segment); | ||
} | ||
if (isKeySegment(segment)) { | ||
return normalizeKeySegment(segment); | ||
} | ||
if (isIndexTuple(segment)) { | ||
return normalizeIndexTupleSegment(segment); | ||
} | ||
return segment; | ||
return isIndexSegment(segment) ? normalizeIndexSegment(segment) : isKeySegment(segment) ? normalizeKeySegment(segment) : isIndexTuple(segment) ? normalizeIndexTupleSegment(segment) : segment; | ||
} | ||
@@ -190,12 +133,27 @@ function normalizeIndexSegment(segment) { | ||
function normalizeKeySegment(segment) { | ||
const segments = segment.match(reKeySegment); | ||
return { | ||
_key: segments[1] | ||
}; | ||
return { _key: segment.match(reKeySegment)[1] }; | ||
} | ||
function normalizeIndexTupleSegment(segment) { | ||
const [from, to] = segment.split(":").map(seg => seg === "" ? seg : Number(seg)); | ||
const [from, to] = segment.split(":").map((seg) => seg === "" ? seg : Number(seg)); | ||
return [from, to]; | ||
} | ||
export { FOCUS_TERMINATOR, _resolveKeyedPath, fromString, get, hasFocus, hasItemFocus, isEqual, isExpanded, isSegmentEqual, numEqualSegments, pathFor, resolveKeyedPath, startsWith, toString, trimChildPath, trimLeft, trimRight }; | ||
export { | ||
FOCUS_TERMINATOR, | ||
_resolveKeyedPath, | ||
fromString, | ||
get, | ||
hasFocus, | ||
hasItemFocus, | ||
isEqual, | ||
isExpanded, | ||
isSegmentEqual, | ||
numEqualSegments, | ||
pathFor, | ||
resolveKeyedPath, | ||
startsWith, | ||
toString, | ||
trimChildPath, | ||
trimLeft, | ||
trimRight | ||
}; | ||
//# sourceMappingURL=paths.esm.js.map |
147
lib/paths.js
@@ -1,16 +0,9 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
var types = require('@sanity/types'); | ||
const rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g; | ||
const reKeySegment = /_key\s*==\s*['"](.*)['"]/; | ||
const EMPTY_PATH = []; | ||
const FOCUS_TERMINATOR = "$"; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: !0 }); | ||
var types = require("@sanity/types"); | ||
const rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g, reKeySegment = /_key\s*==\s*['"](.*)['"]/, EMPTY_PATH = [], FOCUS_TERMINATOR = "$", GROQ_DATA_TYPE_VALUES = ["true", "false", "null"]; | ||
function get(obj, path, defaultVal) { | ||
const select = typeof path === "string" ? fromString(path) : path; | ||
if (!Array.isArray(select)) { | ||
const select = typeof path == "string" ? fromString(path) : path; | ||
if (!Array.isArray(select)) | ||
throw new Error("Path must be an array or a string"); | ||
} | ||
let acc = obj; | ||
@@ -20,34 +13,22 @@ for (let i = 0; i < select.length; i++) { | ||
if (types.isIndexSegment(segment)) { | ||
if (!Array.isArray(acc)) { | ||
if (!Array.isArray(acc)) | ||
return defaultVal; | ||
} | ||
acc = acc[segment]; | ||
} | ||
if (types.isKeySegment(segment)) { | ||
if (!Array.isArray(acc)) { | ||
if (!Array.isArray(acc)) | ||
return defaultVal; | ||
} | ||
acc = acc.find(item => item._key === segment._key); | ||
acc = acc.find((item) => item._key === segment._key); | ||
} | ||
if (typeof segment === "string") { | ||
acc = typeof acc === "object" && acc !== null ? acc[segment] : void 0; | ||
} | ||
if (typeof acc === "undefined") { | ||
if (typeof segment == "string" && (acc = typeof acc == "object" && acc !== null ? acc[segment] : void 0), typeof acc > "u") | ||
return defaultVal; | ||
} | ||
} | ||
return acc; | ||
} | ||
const pathsMemo = /* @__PURE__ */new Map(); | ||
const pathsMemo = /* @__PURE__ */ new Map(); | ||
function pathFor(path) { | ||
if (path.length === 0) { | ||
if (path.length === 0) | ||
return EMPTY_PATH; | ||
} | ||
const asString = toString(path); | ||
if (pathsMemo.has(asString)) { | ||
return pathsMemo.get(asString); | ||
} | ||
pathsMemo.set(asString, path); | ||
Object.freeze(path); | ||
return path; | ||
return pathsMemo.has(asString) ? pathsMemo.get(asString) : (pathsMemo.set(asString, path), Object.freeze(path), path); | ||
} | ||
@@ -59,20 +40,9 @@ function isEqual(path, otherPath) { | ||
const length = Math.min(path.length, otherPath.length); | ||
for (let i = 0; i < length; i++) { | ||
if (!isSegmentEqual(path[i], otherPath[i])) { | ||
for (let i = 0; i < length; i++) | ||
if (!isSegmentEqual(path[i], otherPath[i])) | ||
return i; | ||
} | ||
} | ||
return length; | ||
} | ||
function isSegmentEqual(segmentA, segmentB) { | ||
if (types.isKeySegment(segmentA) && types.isKeySegment(segmentB)) { | ||
return segmentA._key === segmentB._key; | ||
} | ||
if (types.isIndexSegment(segmentA)) { | ||
return Number(segmentA) === Number(segmentB); | ||
} | ||
if (types.isIndexTuple(segmentA) && types.isIndexTuple(segmentB)) { | ||
return segmentA[0] === segmentB[0] && segmentA[1] === segmentB[1]; | ||
} | ||
return segmentA === segmentB; | ||
return types.isKeySegment(segmentA) && types.isKeySegment(segmentB) ? segmentA._key === segmentB._key : types.isIndexSegment(segmentA) ? Number(segmentA) === Number(segmentB) : types.isIndexTuple(segmentA) && types.isIndexTuple(segmentB) ? segmentA[0] === segmentB[0] && segmentA[1] === segmentB[1] : segmentA === segmentB; | ||
} | ||
@@ -94,22 +64,14 @@ function hasFocus(focusPath, path) { | ||
function trimLeft(prefix, path) { | ||
if (prefix.length === 0 || path.length === 0) { | ||
if (prefix.length === 0 || path.length === 0) | ||
return path; | ||
} | ||
const [prefixHead, ...prefixTail] = prefix; | ||
const [pathHead, ...pathTail] = path; | ||
if (!isSegmentEqual(prefixHead, pathHead)) { | ||
return path; | ||
} | ||
return pathFor(trimLeft(prefixTail, pathTail)); | ||
const [prefixHead, ...prefixTail] = prefix, [pathHead, ...pathTail] = path; | ||
return isSegmentEqual(prefixHead, pathHead) ? pathFor(trimLeft(prefixTail, pathTail)) : path; | ||
} | ||
function trimRight(suffix, path) { | ||
const sufLen = suffix.length; | ||
const pathLen = path.length; | ||
if (sufLen === 0 || pathLen === 0) { | ||
const sufLen = suffix.length, pathLen = path.length; | ||
if (sufLen === 0 || pathLen === 0) | ||
return path; | ||
} | ||
let i = 0; | ||
while (i < sufLen && i < pathLen && isSegmentEqual(path[pathLen - i - 1], suffix[sufLen - i - 1])) { | ||
for (; i < sufLen && i < pathLen && isSegmentEqual(path[pathLen - i - 1], suffix[sufLen - i - 1]); ) | ||
i++; | ||
} | ||
return pathFor(path.slice(0, pathLen - i)); | ||
@@ -121,38 +83,28 @@ } | ||
function toString(path) { | ||
if (!Array.isArray(path)) { | ||
if (!Array.isArray(path)) | ||
throw new Error("Path is not an array"); | ||
} | ||
return path.reduce((target, segment, i) => { | ||
const segmentType = typeof segment; | ||
if (segmentType === "number") { | ||
return "".concat(target, "[").concat(segment, "]"); | ||
} | ||
if (segmentType === "string") { | ||
const separator = i === 0 ? "" : "."; | ||
return "".concat(target).concat(separator).concat(segment); | ||
} | ||
if (types.isKeySegment(segment) && segment._key) { | ||
return "".concat(target, '[_key=="').concat(segment._key, '"]'); | ||
} | ||
const isHead = i === 0; | ||
if (typeof segment == "number") | ||
return `${target}[${segment}]`; | ||
if (typeof segment == "string") | ||
return isHead ? segment : GROQ_DATA_TYPE_VALUES.includes(segment) ? `${target}["${segment}"]` : `${target}.${segment}`; | ||
if (types.isKeySegment(segment) && segment._key) | ||
return `${target}[_key=="${segment._key}"]`; | ||
if (Array.isArray(segment)) { | ||
const [from, to] = segment; | ||
return "".concat(target, "[").concat(from, ":").concat(to, "]"); | ||
return `${target}[${from}:${to}]`; | ||
} | ||
throw new Error("Unsupported path segment `".concat(JSON.stringify(segment), "`")); | ||
throw new Error(`Unsupported path segment \`${JSON.stringify(segment)}\``); | ||
}, ""); | ||
} | ||
function _resolveKeyedPath(value, path) { | ||
if (path.length === 0) { | ||
if (path.length === 0) | ||
return path; | ||
} | ||
const [next, ...rest] = path; | ||
if (typeof next === "number") { | ||
if (!Array.isArray(value) || !(next in value)) { | ||
if (typeof next == "number") { | ||
if (!Array.isArray(value) || !(next in value)) | ||
return []; | ||
} | ||
const item = value[next]; | ||
const key = item == null ? void 0 : item._key; | ||
return [typeof key === "string" ? { | ||
_key: item._key | ||
} : next, ..._resolveKeyedPath(item, rest)]; | ||
return [typeof (item == null ? void 0 : item._key) == "string" ? { _key: item._key } : next, ..._resolveKeyedPath(item, rest)]; | ||
} | ||
@@ -163,28 +115,16 @@ const nextVal = get(value, [next]); | ||
function resolveKeyedPath(value, path) { | ||
if (!Array.isArray(path)) { | ||
if (!Array.isArray(path)) | ||
throw new Error("Path is not an array"); | ||
} | ||
return pathFor(_resolveKeyedPath(value, path)); | ||
} | ||
function fromString(path) { | ||
if (typeof path !== "string") { | ||
if (typeof path != "string") | ||
throw new Error("Path is not a string"); | ||
} | ||
const segments = path.match(rePropName); | ||
if (!segments) { | ||
if (!segments) | ||
throw new Error("Invalid path string"); | ||
} | ||
return segments.map(normalizePathSegment); | ||
} | ||
function normalizePathSegment(segment) { | ||
if (types.isIndexSegment(segment)) { | ||
return normalizeIndexSegment(segment); | ||
} | ||
if (types.isKeySegment(segment)) { | ||
return normalizeKeySegment(segment); | ||
} | ||
if (types.isIndexTuple(segment)) { | ||
return normalizeIndexTupleSegment(segment); | ||
} | ||
return segment; | ||
return types.isIndexSegment(segment) ? normalizeIndexSegment(segment) : types.isKeySegment(segment) ? normalizeKeySegment(segment) : types.isIndexTuple(segment) ? normalizeIndexTupleSegment(segment) : segment; | ||
} | ||
@@ -195,9 +135,6 @@ function normalizeIndexSegment(segment) { | ||
function normalizeKeySegment(segment) { | ||
const segments = segment.match(reKeySegment); | ||
return { | ||
_key: segments[1] | ||
}; | ||
return { _key: segment.match(reKeySegment)[1] }; | ||
} | ||
function normalizeIndexTupleSegment(segment) { | ||
const [from, to] = segment.split(":").map(seg => seg === "" ? seg : Number(seg)); | ||
const [from, to] = segment.split(":").map((seg) => seg === "" ? seg : Number(seg)); | ||
return [from, to]; | ||
@@ -204,0 +141,0 @@ } |
{ | ||
"name": "@sanity/util", | ||
"version": "3.34.1-cds-unstable.22+92e403c791", | ||
"version": "3.35.0", | ||
"description": "Utilities shared across projects of Sanity", | ||
@@ -165,4 +165,4 @@ "keywords": [ | ||
"dependencies": { | ||
"@sanity/client": "^6.15.4", | ||
"@sanity/types": "3.34.1-cds-unstable.22+92e403c791", | ||
"@sanity/client": "^6.15.6", | ||
"@sanity/types": "3.35.0", | ||
"get-random-values-esm": "1.0.2", | ||
@@ -180,3 +180,3 @@ "moment": "^2.29.4", | ||
}, | ||
"gitHead": "92e403c791dff5741f66ef4db6bac160ad2dee73" | ||
"gitHead": "e65237b6fac8113616e8aea96b8df4cc2d12607e" | ||
} |
@@ -18,2 +18,6 @@ import { | ||
// Fields named as GROQ data types cannot be accessed using dot notation. These fields must instead | ||
// be serialized using square bracket notation. | ||
const GROQ_DATA_TYPE_VALUES = ['true', 'false', 'null'] | ||
export function get<R>(obj: unknown, path: Path | string): R | undefined | ||
@@ -168,10 +172,18 @@ export function get<R>(obj: unknown, path: Path | string, defaultValue: R): R | ||
return path.reduce<string>((target, segment, i) => { | ||
const segmentType = typeof segment | ||
if (segmentType === 'number') { | ||
const isHead = i === 0 | ||
if (typeof segment === 'number') { | ||
return `${target}[${segment}]` | ||
} | ||
if (segmentType === 'string') { | ||
const separator = i === 0 ? '' : '.' | ||
return `${target}${separator}${segment}` | ||
if (typeof segment === 'string') { | ||
if (isHead) { | ||
return segment | ||
} | ||
if (GROQ_DATA_TYPE_VALUES.includes(segment)) { | ||
return `${target}["${segment}"]` | ||
} | ||
return `${target}.${segment}` | ||
} | ||
@@ -178,0 +190,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
0
128630
1704
+ Added@sanity/types@3.35.0(transitive)
+ Added@types/prop-types@15.7.12(transitive)
+ Added@types/react@18.3.4(transitive)
+ Addedcsstype@3.1.3(transitive)
Updated@sanity/client@^6.15.6
Updated@sanity/types@3.35.0