@magicspace/core
Advanced tools
Comparing version 0.2.26 to 0.2.27
@@ -29,3 +29,3 @@ /// <reference types="node" /> | ||
} | ||
export declare function handlebars<TData>(path: string, data: TData, options?: HandlebarsOptions): Composable<string, TextFileOptions>; | ||
export declare function handlebars<TData>(data: TData, options?: HandlebarsOptions): Composable<string, TextFileOptions>; | ||
export declare function handlebars<TData extends object>(path: string, data: TData, options?: HandlebarsOptions): Composable<string, TextFileOptions>; | ||
export declare function handlebars<TData extends object>(data: TData, options?: HandlebarsOptions): Composable<string, TextFileOptions>; |
@@ -0,7 +1,5 @@ | ||
import { GeneralSortObjectKeysOptions } from '@magicspace/utils'; | ||
import { File } from '../file'; | ||
export interface StructuredFileOptions { | ||
/** | ||
* Sort content keys. A string array can be specified as a shortcut of `top`. | ||
*/ | ||
sortKeys?: StructuredFileSortKeysOptions | string[]; | ||
sortKeys?: GeneralSortObjectKeysOptions; | ||
} | ||
@@ -12,7 +10,1 @@ export declare abstract class StructuredFile<TContent, TOptions extends StructuredFileOptions> extends File<TContent, TOptions> { | ||
} | ||
export interface StructuredFileSortKeysOptions { | ||
top?: string[]; | ||
bottom?: string[]; | ||
compare?(left: string, right: string): number; | ||
deep?: boolean; | ||
} |
@@ -5,3 +5,4 @@ "use strict"; | ||
const tslib_1 = require("tslib"); | ||
const sort_keys_1 = tslib_1.__importDefault(require("sort-keys")); | ||
const utils_1 = require("@magicspace/utils"); | ||
const lodash_1 = tslib_1.__importDefault(require("lodash")); | ||
const file_1 = require("../file"); | ||
@@ -12,33 +13,4 @@ class StructuredFile extends file_1.File { | ||
let content = this.content; | ||
if (sortKeysOptions) { | ||
if (Array.isArray(sortKeysOptions)) { | ||
sortKeysOptions = { top: sortKeysOptions }; | ||
} | ||
let { top, bottom, compare, deep } = sortKeysOptions; | ||
let topKeyToIndexMap = new Map(top === null || top === void 0 ? void 0 : top.map((key, index) => [key, index])); | ||
let bottomKeyToIndexMap = new Map(bottom === null || bottom === void 0 ? void 0 : bottom.map((key, index) => [key, index])); | ||
content = sort_keys_1.default(content, { | ||
compare(left, right) { | ||
if (topKeyToIndexMap.has(left)) { | ||
if (topKeyToIndexMap.has(right)) { | ||
return topKeyToIndexMap.get(left) - topKeyToIndexMap.get(right); | ||
} | ||
return -1; | ||
} | ||
else if (topKeyToIndexMap.has(right)) { | ||
return 1; | ||
} | ||
if (bottomKeyToIndexMap.has(left)) { | ||
if (bottomKeyToIndexMap.has(right)) { | ||
return (bottomKeyToIndexMap.get(left) - bottomKeyToIndexMap.get(right)); | ||
} | ||
return 1; | ||
} | ||
else if (bottomKeyToIndexMap.has(right)) { | ||
return -1; | ||
} | ||
return compare ? compare(left, right) : 0; | ||
}, | ||
deep, | ||
}); | ||
if (sortKeysOptions && lodash_1.default.isPlainObject(content)) { | ||
content = utils_1.sortObjectKeys(content, sortKeysOptions); | ||
} | ||
@@ -45,0 +17,0 @@ return this.stringify(content); |
{ | ||
"name": "@magicspace/core", | ||
"version": "0.2.26", | ||
"version": "0.2.27", | ||
"publishConfig": { | ||
@@ -18,2 +18,3 @@ "access": "public" | ||
"dependencies": { | ||
"@magicspace/utils": "^0.2.27", | ||
"fast-glob": "^3.2.4", | ||
@@ -25,3 +26,2 @@ "fs-extra": "^9.0.1", | ||
"npm-path": "^2.0.4", | ||
"sort-keys": "^4.0.0", | ||
"tiva": "^0.2.1", | ||
@@ -38,3 +38,3 @@ "tmp": "^0.2.1", | ||
}, | ||
"gitHead": "6c07ca54091234dc76bda2b83fde47e119b6c40c" | ||
"gitHead": "9d7ead95f5674bfe4ad9788812fed361f8c608c6" | ||
} |
@@ -150,3 +150,3 @@ import * as FS from 'fs'; | ||
export function handlebars<TData>( | ||
export function handlebars<TData extends object>( | ||
path: string, | ||
@@ -156,3 +156,3 @@ data: TData, | ||
): Composable<string, TextFileOptions>; | ||
export function handlebars<TData>( | ||
export function handlebars<TData extends object>( | ||
data: TData, | ||
@@ -159,0 +159,0 @@ options?: HandlebarsOptions, |
@@ -1,2 +0,3 @@ | ||
import sortKeys from 'sort-keys'; | ||
import {GeneralSortObjectKeysOptions, sortObjectKeys} from '@magicspace/utils'; | ||
import _ from 'lodash'; | ||
@@ -6,6 +7,3 @@ import {File} from '../file'; | ||
export interface StructuredFileOptions { | ||
/** | ||
* Sort content keys. A string array can be specified as a shortcut of `top`. | ||
*/ | ||
sortKeys?: StructuredFileSortKeysOptions | string[]; | ||
sortKeys?: GeneralSortObjectKeysOptions; | ||
} | ||
@@ -24,42 +22,4 @@ | ||
if (sortKeysOptions) { | ||
if (Array.isArray(sortKeysOptions)) { | ||
sortKeysOptions = {top: sortKeysOptions}; | ||
} | ||
let {top, bottom, compare, deep} = sortKeysOptions; | ||
let topKeyToIndexMap = new Map(top?.map((key, index) => [key, index])); | ||
let bottomKeyToIndexMap = new Map( | ||
bottom?.map((key, index) => [key, index]), | ||
); | ||
content = sortKeys(content, { | ||
compare(left, right) { | ||
if (topKeyToIndexMap.has(left)) { | ||
if (topKeyToIndexMap.has(right)) { | ||
return topKeyToIndexMap.get(left)! - topKeyToIndexMap.get(right)!; | ||
} | ||
return -1; | ||
} else if (topKeyToIndexMap.has(right)) { | ||
return 1; | ||
} | ||
if (bottomKeyToIndexMap.has(left)) { | ||
if (bottomKeyToIndexMap.has(right)) { | ||
return ( | ||
bottomKeyToIndexMap.get(left)! - bottomKeyToIndexMap.get(right)! | ||
); | ||
} | ||
return 1; | ||
} else if (bottomKeyToIndexMap.has(right)) { | ||
return -1; | ||
} | ||
return compare ? compare(left, right) : 0; | ||
}, | ||
deep, | ||
}); | ||
if (sortKeysOptions && _.isPlainObject(content)) { | ||
content = sortObjectKeys(content as any, sortKeysOptions); | ||
} | ||
@@ -70,8 +30,1 @@ | ||
} | ||
export interface StructuredFileSortKeysOptions { | ||
top?: string[]; | ||
bottom?: string[]; | ||
compare?(left: string, right: string): number; | ||
deep?: boolean; | ||
} |
Sorry, the diff of this file is not supported yet
124797
2690
+ Added@magicspace/utils@^0.2.27
+ Added@magicspace/utils@0.2.36(transitive)
+ Added@sindresorhus/is@0.14.0(transitive)
+ Added@szmarczak/http-timer@1.1.2(transitive)
+ Addedansi-styles@4.3.0(transitive)
+ Addedcacheable-request@6.1.0(transitive)
+ Addedchalk@4.1.2(transitive)
+ Addedclone-response@1.0.3(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addeddecompress-response@3.3.0(transitive)
+ Addeddeep-extend@0.6.0(transitive)
+ Addeddefer-to-connect@1.1.3(transitive)
+ Addedduplexer3@0.1.5(transitive)
+ Addedend-of-stream@1.4.4(transitive)
+ Addedget-stream@4.1.05.2.0(transitive)
+ Addedgot@9.6.0(transitive)
+ Addedhas-flag@4.0.0(transitive)
+ Addedhttp-cache-semantics@4.1.1(transitive)
+ Addedini@1.3.8(transitive)
+ Addedjson-buffer@3.0.0(transitive)
+ Addedkeyv@3.1.0(transitive)
+ Addedlatest-version@5.1.0(transitive)
+ Addedlowercase-keys@1.0.12.0.0(transitive)
+ Addedmimic-response@1.0.1(transitive)
+ Addednormalize-url@4.5.1(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedp-cancelable@1.1.0(transitive)
+ Addedpackage-json@6.5.0(transitive)
+ Addedprepend-http@2.0.0(transitive)
+ Addedpump@3.0.2(transitive)
+ Addedrc@1.2.8(transitive)
+ Addedregistry-auth-token@4.2.2(transitive)
+ Addedregistry-url@5.1.0(transitive)
+ Addedresponselike@1.0.2(transitive)
+ Addedsemver@6.3.1(transitive)
+ Addedstrip-json-comments@2.0.1(transitive)
+ Addedsupports-color@7.2.0(transitive)
+ Addedto-readable-stream@1.0.0(transitive)
+ Addedurl-parse-lax@3.0.0(transitive)
+ Addedwrappy@1.0.2(transitive)
- Removedsort-keys@^4.0.0
- Removedis-plain-obj@2.1.0(transitive)
- Removedsort-keys@4.2.0(transitive)