Skip to content

Commit

Permalink
refactor: update utils (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivodolenc authored May 8, 2023
1 parent e706ad4 commit 4155e6a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
6 changes: 3 additions & 3 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineNuxtModule, addTemplate, addPluginTemplate } from '@nuxt/kit'
import { stringify } from './utils'
import { serialize } from './utils'
import { name, version, configKey } from '../package.json'
import type { ModuleOptions } from './types'

Expand Down Expand Up @@ -85,14 +85,14 @@ export default defineNuxtModule<ModuleOptions>({
// Global Effects
if (regEffects)
regEffects.forEach(effect =>
pluginEffect.push(`gsap.registerEffect(${stringify(effect)});`)
pluginEffect.push(`gsap.registerEffect(${serialize(effect)});`)
)

// Global Eases
if (regEases)
regEases.forEach(ease =>
pluginEase.push(
`gsap.registerEase(${stringify(ease.name)}, ${stringify(ease.ease)});`
`gsap.registerEase(${serialize(ease.name)}, ${serialize(ease.ease)});`
)
)

Expand Down
24 changes: 10 additions & 14 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
/**
* Transforms any data type to a string.
* Serializes data.
*
* @since 2.2.0
*/
export function stringify(data: unknown): string {
if (data === undefined || data === null)
return data === undefined ? 'undefined' : 'null'
export function serialize(data: unknown): string {
const type = (v: string) => typeof data === v

if (typeof data === 'string') return `'${data}'`
if (data === null || data === undefined)
return data === null ? 'null' : 'undefined'

if (
typeof data === 'number' ||
typeof data === 'boolean' ||
typeof data === 'function'
)
if (type('boolean') || type('number') || type('function'))
return data.toString()

if (data instanceof Array) return `[${data.map(v => stringify(v)).join(',')}]`
if (Array.isArray(data)) return `[${data.map(v => serialize(v)).join(',')}]`

if (typeof data === 'object')
if (type('object'))
return `{${Object.entries(data)
.map(([key, val]) => `${key}: ${stringify(val)}`)
.map(([key, val]) => `${key}: ${serialize(val)}`)
.join(',')}}`

return JSON.stringify(data)
return `'${data}'`
}

0 comments on commit 4155e6a

Please sign in to comment.