Skip to content

Commit

Permalink
refactor: move a few shared utilities form form-builder to util package
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge committed Apr 28, 2021
1 parent 5f8977d commit a391a1c
Show file tree
Hide file tree
Showing 20 changed files with 71 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react'
import Fieldset from 'part:@sanity/components/fieldsets/default'
import {resolveTypeName} from '@sanity/util/content'
import {FormBuilderInput} from '../../FormBuilderInput'
import InvalidValue from '../InvalidValueInput'
import {resolveTypeName} from '../../utils/resolveTypeName'
import styles from './styles/Field.css'

type FieldProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import React from 'react'
import {Marker, ObjectSchemaTypeWithOptions, Path} from '@sanity/types'
import {FormFieldPresence} from '@sanity/base/presence'
import Fieldset from 'part:@sanity/components/fieldsets/default'
import {isEmpty} from '@sanity/util/content'
import PatchEvent, {set, setIfMissing, unset} from '../../PatchEvent'
import isEmpty from '../../utils/isEmpty'
import Field from './Field'
import UnknownFields from './UnknownFields'
import fieldStyles from './styles/Field.css'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import {Button, Stack, Text} from '@sanity/ui'
import React from 'react'
import {map} from 'rxjs/operators'
import {Subscription} from 'rxjs'
import {randomKey, resolveTypeName} from '@sanity/util/content'
import {ArrayFunctions} from '../../../legacyParts'
import {insert, PatchEvent, set, setIfMissing, unset} from '../../../PatchEvent'
import {ResolvedUploader, Uploader, UploadEvent} from '../../../sanity/uploads/types'
import {resolveTypeName} from '../../../utils/resolveTypeName'
import {Alert} from '../../../components/Alert'
import {Details} from '../../../components/Details'
import randomKey from '../common/randomKey'
import {Item, List} from '../common/list'
import {EMPTY_ARRAY} from '../../../utils/empty'
import {ArrayItem} from './item'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Box, Card, Flex, Popover, Text, useClickOutside} from '@sanity/ui'
import React from 'react'
import {BulbOutlineIcon, UnknownIcon} from '@sanity/icons'
import {resolveTypeName} from '../../../../utils/resolveTypeName'
import {resolveTypeName} from '@sanity/util/content'

type Props = {value: any; onFocus?: () => void}
export function ItemWithMissingType(props: Props) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {ArraySchemaType, SchemaType} from '@sanity/types'
import {resolveTypeName} from '../../../../utils/resolveTypeName'
import {resolveTypeName} from '@sanity/util/content'
import {IGNORE_KEYS} from './constants'

// eslint-disable-next-line @typescript-eslint/ban-types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {ArraySchemaType, Marker, Path, SchemaType} from '@sanity/types'
import {Stack} from '@sanity/ui'
import {FormFieldSet} from '@sanity/base/components'
import {FormFieldPresence} from '@sanity/base/presence'
import {resolveTypeName} from '@sanity/util/content'
import {PatchEvent, set, unset} from '../../../PatchEvent'
import {resolveTypeName} from '../../../utils/resolveTypeName'
import {Item, List} from '../common/list'
import {ArrayFunctions} from '../../../legacyParts'
import getEmptyValue from './getEmptyValue'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {FormFieldSet} from '@sanity/base/components'
import {ArraySchemaType, isTitledListValue, Marker, Path} from '@sanity/types'
import {Box, Checkbox, Flex, Text} from '@sanity/ui'
import {FormFieldPresence} from '@sanity/base/lib/presence'
import {resolveTypeName} from '@sanity/util/content'
import PatchEvent, {set, unset} from '../../../PatchEvent'
import {resolveTypeName} from '../../../utils/resolveTypeName'
import Preview from '../../../Preview'
import {ItemWithMissingType} from '../ArrayOfObjectsInput/item/ItemWithMissingType'
import {Item, List} from '../common/list'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@ function whatwgRNG(length = 16) {
return rnds8
}

const byteToHex: string[] = []
for (let i = 0; i < 256; ++i) {
byteToHex[i] = (i + 0x100).toString(16).substring(1)
}
const getByteHexTable = (() => {
let table: string[]
return () => {
if (table) {
return table
}
table = []
for (let i = 0; i < 256; ++i) {
table[i] = (i + 0x100).toString(16).substring(1)
}
return table
}
})()

export default function randomKey(length?: number) {
const table = getByteHexTable()
return whatwgRNG(length)
.reduce((str, n) => str + byteToHex[n], '')
.reduce((str, n) => str + table[n], '')
.slice(0, length)
}
3 changes: 2 additions & 1 deletion packages/@sanity/form-builder/src/patch/array.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import hasOwn from '../utils/hasOwn'
import {findIndex} from 'lodash'
import applyPatch from './applyPatch'
import insert from './arrayInsert'

const hasOwn = (obj, property) => Object.prototype.hasOwnProperty.call(obj, property)

function move(arr, from, to) {
const nextValue = arr.slice()
const val = nextValue[from]
Expand Down
3 changes: 3 additions & 0 deletions packages/@sanity/util/content.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as contentUtils from './src/contentUtils'

export = contentUtils
1 change: 1 addition & 0 deletions packages/@sanity/util/content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/contentUtils')
2 changes: 1 addition & 1 deletion packages/@sanity/util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@sanity/types": "2.9.1",
"dotenv": "^8.2.0",
"fs-extra": "^6.0.1",
"get-random-values": "^1.2.0",
"get-random-values": "^1.2.2",
"lodash": "^4.17.15",
"resolve-from": "^4.0.0"
},
Expand Down
3 changes: 3 additions & 0 deletions packages/@sanity/util/src/contentUtils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './isEmpty'
export {randomKey} from './randomKey'
export {resolveTypeName} from './resolveTypeName'
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import hasOwn from './hasOwn'

function isEmptyObject(value: {[key: string]: any}): boolean {
export function isEmptyObject(value: {[key: string]: any}): boolean {
for (const key in value) {
if (key === '_type' || key === '_key') {
continue
Expand All @@ -12,7 +12,7 @@ function isEmptyObject(value: {[key: string]: any}): boolean {
return true
}

function isEmptyArray(value: Array<any>): boolean {
export function isEmptyArray(value: unknown[]): boolean {
for (let i = 0; i < value.length; i++) {
if (isEmpty(value[i])) {
return true
Expand All @@ -21,7 +21,7 @@ function isEmptyArray(value: Array<any>): boolean {
return false
}

export default function isEmpty(value: any): boolean {
export function isEmpty(value: any): boolean {
if (value === undefined || value === null) {
return true
}
Expand Down
30 changes: 30 additions & 0 deletions packages/@sanity/util/src/contentUtils/randomKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import getRandomValues from 'get-random-values'

const getByteHexTable = (() => {
let table
return () => {
if (table) {
return table
}

table = []
for (let i = 0; i < 256; ++i) {
table[i] = (i + 0x100).toString(16).substring(1)
}
return table
}
})()

// WHATWG crypto RNG - https://w3c.github.io/webcrypto/Overview.html
function whatwgRNG(length = 16) {
const rnds8 = new Uint8Array(length)
getRandomValues(rnds8)
return rnds8
}

export function randomKey(length?: number): string {
const table = getByteHexTable()
return whatwgRNG(length)
.reduce((str, n) => str + table[n], '')
.slice(0, length)
}
34 changes: 5 additions & 29 deletions packages/@sanity/util/src/pathUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import getRandomValues from 'get-random-values'
import {
IndexTuple,
isIndexSegment,
Expand Down Expand Up @@ -224,31 +223,8 @@ function normalizeIndexTupleSegment(segment: string): IndexTuple {
return [from, to]
}

const getByteHexTable = (() => {
let table
return () => {
if (table) {
return table
}

table = []
for (let i = 0; i < 256; ++i) {
table[i] = (i + 0x100).toString(16).substring(1)
}
return table
}
})()

// WHATWG crypto RNG - https://w3c.github.io/webcrypto/Overview.html
function whatwgRNG(length = 16) {
const rnds8 = new Uint8Array(length)
getRandomValues(rnds8)
return rnds8
}

export function randomKey(length?: number): string {
const table = getByteHexTable()
return whatwgRNG(length)
.reduce((str, n) => str + table[n], '')
.slice(0, length)
}
/**
*
* @deprecated use import {randomKey} from '@sanity/util/content instead
*/
export {randomKey} from './contentUtils'
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@sanity/structure": ["./@sanity/structure/src"],
"@sanity/transaction-collator": ["./@sanity/transaction-collator/src"],
"@sanity/util/paths": ["./@sanity/util/src/pathUtils"],
"@sanity/util/content": ["./@sanity/util/src/contentUtils"],
"@sanity/util": ["./@sanity/util/src"],
"@sanity/types": ["./@sanity/types/src"],
"@sanity/validation": ["./@sanity/validation/src"]
Expand Down

0 comments on commit a391a1c

Please sign in to comment.