diff --git a/lib/api.js b/lib/api.js index 9309812..e01eb61 100644 --- a/lib/api.js +++ b/lib/api.js @@ -1,8 +1,9 @@ import axios from 'axios' import debounce from 'lodash.debounce' +import omitBy from 'lodash.omitby' import ms from 'ms' -import { DEFAULT_CODE } from './constants' +import { DEFAULT_CODE, DEFAULT_SETTINGS } from './constants' import { fileToDataURL } from './util' @@ -82,11 +83,18 @@ function getSnippet(uid, { host } = {}) { }) } +function isNotDefaultSetting(v, k) { + return v === DEFAULT_SETTINGS[k] || !Object.prototype.hasOwnProperty.call(DEFAULT_SETTINGS, k) +} + function updateSnippet(uid, state) { + const sanitized = omitBy(state, isNotDefaultSetting) + const data = { - ...state, + ...sanitized, code: state.code != null ? state.code : DEFAULT_CODE } + if (uid) { return client .patch(`/snippets/${uid}`, data)