Skip to content

Commit

Permalink
feat(@uform/utils): support ts, but build scripts is not work
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed May 15, 2019
1 parent 04e96b4 commit 8e45214
Show file tree
Hide file tree
Showing 23 changed files with 128 additions and 1,478 deletions.
483 changes: 0 additions & 483 deletions packages/utils/src/accessor.js

This file was deleted.

20 changes: 10 additions & 10 deletions packages/utils/src/accessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ type TokenizerHandlers = {
destructKey(str: string, isColon?: boolean): void
}

export type Path = Array<string | number> | string

type Destruct = {
[key: string]: string
} | Path

type TraverseCallback = (path: string[], item: any) => void

type Path = Path

type Getter = (obj: any, path: Path, value?: any) => any

type Setter = (obj: any, path: Path, value?: any) => any
Expand Down Expand Up @@ -274,7 +274,7 @@ const parseDestruct = (string: string | number) => {
}

const traverse = (obj: any, callback: TraverseCallback) => {
const _traverse = (obj: any, path: Path, callback: TraverseCallback) => {
const _traverse = (obj: any, path: string[], callback: TraverseCallback) => {
if (isStr(obj)) return callback(obj, obj)
each(obj, (item: any, key: string) => {
const _path = path.concat(key)
Expand All @@ -290,7 +290,7 @@ const traverse = (obj: any, callback: TraverseCallback) => {
}

const mapReduce = (obj: any, callback: TraverseCallback) => {
const _traverse = (obj: any, path: Path, callback: TraverseCallback) => {
const _traverse = (obj: any, path: string[], callback: TraverseCallback) => {
return map(obj, (item: any, key: string) => {
const _path = path.concat(key)
if (isArr(item) || isPlainObj(item)) {
Expand All @@ -304,7 +304,7 @@ const mapReduce = (obj: any, callback: TraverseCallback) => {
return _traverse(obj, [], callback)
}

const parseDesturctPath = (path: Path) => {
const parseDesturctPath = (path: Path): any => {
const _path = getPathSegments(path)
const lastKey = _path[_path.length - 1]
const startPath = _path.slice(0, _path.length - 1)
Expand All @@ -317,7 +317,7 @@ const parseDesturctPath = (path: Path) => {
}
}

const parsePaths = (path: Path) => {
const parsePaths = (path: Path): any => {
const result = []
const parsed = parseDesturctPath(path)
if (isStr(parsed.destruct)) {
Expand All @@ -339,7 +339,7 @@ const parsePaths = (path: Path) => {

const resolveGetIn = (get: Getter) => {
const cache = new Map()
return (obj: any, path: Path, value: any): any => {
return (obj: any, path: Path, value?: any): any => {
let ast = null
if (!(ast = cache.get(path))) {
ast = parseDesturctPath(path)
Expand Down Expand Up @@ -401,7 +401,7 @@ function _getIn(obj: any, path: Path, value: any) {
path = toString(path)

if (path in obj) {
return obj[path]
return obj[path as string]
}

const pathArr = getPathSegments(path)
Expand Down Expand Up @@ -438,7 +438,7 @@ function _setIn(obj: any, path: Path, value: any) {
path = toString(path)

if (path in obj) {
obj[path] = value
obj[path as string] = value
return
}

Expand Down Expand Up @@ -467,7 +467,7 @@ function _deleteIn(obj: any, path: Path) {
path = toString(path)

if (path in obj) {
delete obj[path]
delete obj[path as string]
return
}

Expand Down
126 changes: 0 additions & 126 deletions packages/utils/src/array.js

This file was deleted.

11 changes: 7 additions & 4 deletions packages/utils/src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,30 @@ import { isArr } from './types'

type EachCallback = (item: any, key: string | number) => void | boolean

type ReduceCallback = (buffer: any, item: any, key: string | number) => void | boolean

type ArrayLike = object | Array<any>

export const toArr = (val: any): Array<any> => (isArr(val) ? val : val ? [val] : [])

export const each = (val: ArrayLike, callback: EachCallback, revert?: boolean) => {
if (isArr(val)) {
if (revert) {
for (let i = (val as Array<any>).length - 1; i >= 0; i--) {
for (let i: number = (val as Array<any>).length - 1; i >= 0; i--) {
if (callback(val[i], i) === false) {
return
}
}
} else {
for (let i = 0, length = (val as Array<any>).length; i < length; i++) {
for (let i: number = 0, length = (val as Array<any>).length; i < length; i++) {
if (callback(val[i], i) === false) {
return
}
}
}
} else {
for (let key in val) {
let key: string
for (key in val) {
if (Object.hasOwnProperty.call(val, key)) {
if (callback(val[key], key) === false) {
return
Expand All @@ -49,7 +52,7 @@ export const map = (val: ArrayLike, callback: EachCallback, revert?: boolean): A
return res
}

export const reduce = (val: ArrayLike, callback: EachCallback, initial: any, revert?: boolean): any => {
export const reduce = (val: ArrayLike, callback: ReduceCallback, initial: any, revert?: boolean): any => {
let res = initial
each(
val,
Expand Down
56 changes: 0 additions & 56 deletions packages/utils/src/broadcast.js

This file was deleted.

17 changes: 11 additions & 6 deletions packages/utils/src/broadcast.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { each } from './array'
import { isFn } from './types'

type Subscriber = (notification: any) => void

type Filter = (payload: any, subscription: any) => any

export class Broadcast {
entries = []
buffer = []
private entries = []
private buffer = []
private length: number

subscribe(subscriber, subscription) {
subscribe(subscriber: Subscriber, subscription: any) {
if (!isFn(subscriber)) return () => { }
let index = this.entries.length
this.entries.push({
Expand All @@ -26,7 +31,7 @@ export class Broadcast {
flushBuffer({ subscriber, subscription }) {
each(this.buffer, ({ payload, filter }) => {
if (isFn(filter)) {
let notification
let notification: any
if ((notification = filter(payload, subscription))) {
subscriber(notification)
}
Expand All @@ -36,14 +41,14 @@ export class Broadcast {
})
}

notify(payload, filter) {
notify(payload: any, filter: Filter) {
if (this.length === 0) {
this.buffer.push({ payload, filter })
return
}
each(this.entries, ({ subscriber, subscription }) => {
if (isFn(filter)) {
let notification
let notification: any
if ((notification = filter(payload, subscription))) {
subscriber(notification)
}
Expand Down
3 changes: 0 additions & 3 deletions packages/utils/src/case.js

This file was deleted.

6 changes: 4 additions & 2 deletions packages/utils/src/case.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const camelCase = require('camel-case')
import camelCase from 'camel-case'
export { camelCase }

export const lowercase = str => String(str || '').toLowerCase()

export const lowercase = (str: any) => String(str || '').toLowerCase()
Loading

0 comments on commit 8e45214

Please sign in to comment.