1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-27 17:49:08 -05:00

remove unnecessary path.resolve in move/readJson/writeJson (#292)

This commit is contained in:
Axetroy 2019-03-20 01:23:22 +08:00 committed by Ryan Dahl
parent 59adafe867
commit a00c51b05b
4 changed files with 16 additions and 19 deletions

View file

@ -1,5 +1,4 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import * as path from "./path/mod.ts";
import { exists, existsSync } from "./exists.ts"; import { exists, existsSync } from "./exists.ts";
import { isSubdir } from "./utils.ts"; import { isSubdir } from "./utils.ts";
@ -13,9 +12,6 @@ export async function move(
dest: string, dest: string,
options?: MoveOptions options?: MoveOptions
): Promise<void> { ): Promise<void> {
src = path.resolve(src);
dest = path.resolve(dest);
const srcStat = await Deno.stat(src); const srcStat = await Deno.stat(src);
if (srcStat.isDirectory() && isSubdir(src, dest)) { if (srcStat.isDirectory() && isSubdir(src, dest)) {
@ -43,9 +39,6 @@ export function moveSync(
dest: string, dest: string,
options?: MoveOptions options?: MoveOptions
): void { ): void {
src = path.resolve(src);
dest = path.resolve(dest);
const srcStat = Deno.statSync(src); const srcStat = Deno.statSync(src);
if (srcStat.isDirectory() && isSubdir(src, dest)) { if (srcStat.isDirectory() && isSubdir(src, dest)) {

View file

@ -1,9 +1,7 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import * as path from "./path/mod.ts";
/** Reads a JSON file and then parses it into an object */ /** Reads a JSON file and then parses it into an object */
export async function readJson(filePath: string): Promise<any> { export async function readJson(filePath: string): Promise<any> {
filePath = path.resolve(filePath);
const decoder = new TextDecoder("utf-8"); const decoder = new TextDecoder("utf-8");
const content = decoder.decode(await Deno.readFile(filePath)); const content = decoder.decode(await Deno.readFile(filePath));
@ -18,7 +16,6 @@ export async function readJson(filePath: string): Promise<any> {
/** Reads a JSON file and then parses it into an object */ /** Reads a JSON file and then parses it into an object */
export function readJsonSync(filePath: string): any { export function readJsonSync(filePath: string): any {
filePath = path.resolve(filePath);
const decoder = new TextDecoder("utf-8"); const decoder = new TextDecoder("utf-8");
const content = decoder.decode(Deno.readFileSync(filePath)); const content = decoder.decode(Deno.readFileSync(filePath));

View file

@ -34,7 +34,7 @@ test(async function readInvalidJsonFile() {
}); });
}); });
test(async function readValidJsonFile() { test(async function readValidArrayJsonFile() {
const invalidJsonFile = path.join(testdataDir, "json_valid_array.json"); const invalidJsonFile = path.join(testdataDir, "json_valid_array.json");
const json = await readJson(invalidJsonFile); const json = await readJson(invalidJsonFile);
@ -42,7 +42,7 @@ test(async function readValidJsonFile() {
assertEquals(json, ["1", "2", "3"]); assertEquals(json, ["1", "2", "3"]);
}); });
test(async function readValidJsonFile() { test(async function readValidObjJsonFile() {
const invalidJsonFile = path.join(testdataDir, "json_valid_obj.json"); const invalidJsonFile = path.join(testdataDir, "json_valid_obj.json");
const json = await readJson(invalidJsonFile); const json = await readJson(invalidJsonFile);
@ -50,6 +50,12 @@ test(async function readValidJsonFile() {
assertEquals(json, { key1: "value1", key2: "value2" }); assertEquals(json, { key1: "value1", key2: "value2" });
}); });
test(async function readValidObjJsonFileWithRelativePath() {
const json = await readJson("./fs/testdata/json_valid_obj.json");
assertEquals(json, { key1: "value1", key2: "value2" });
});
test(function readJsonFileNotExistsSync() { test(function readJsonFileNotExistsSync() {
const emptyJsonFile = path.join(testdataDir, "json_not_exists.json"); const emptyJsonFile = path.join(testdataDir, "json_not_exists.json");
@ -74,7 +80,7 @@ test(function readInvalidJsonFile() {
}); });
}); });
test(function readValidJsonFile() { test(function readValidArrayJsonFileSync() {
const invalidJsonFile = path.join(testdataDir, "json_valid_array.json"); const invalidJsonFile = path.join(testdataDir, "json_valid_array.json");
const json = readJsonSync(invalidJsonFile); const json = readJsonSync(invalidJsonFile);
@ -82,10 +88,16 @@ test(function readValidJsonFile() {
assertEquals(json, ["1", "2", "3"]); assertEquals(json, ["1", "2", "3"]);
}); });
test(function readValidJsonFile() { test(function readValidObjJsonFileSync() {
const invalidJsonFile = path.join(testdataDir, "json_valid_obj.json"); const invalidJsonFile = path.join(testdataDir, "json_valid_obj.json");
const json = readJsonSync(invalidJsonFile); const json = readJsonSync(invalidJsonFile);
assertEquals(json, { key1: "value1", key2: "value2" }); assertEquals(json, { key1: "value1", key2: "value2" });
}); });
test(function readValidObjJsonFileSyncWithRelativePath() {
const json = readJsonSync("./fs/testdata/json_valid_obj.json");
assertEquals(json, { key1: "value1", key2: "value2" });
});

View file

@ -1,6 +1,5 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-explicit-any */
import * as path from "./path/mod.ts";
type Replacer = (key: string, value: any) => any; type Replacer = (key: string, value: any) => any;
export interface WriteJsonOptions { export interface WriteJsonOptions {
@ -14,8 +13,6 @@ export async function writeJson(
object: any, object: any,
options: WriteJsonOptions = {} options: WriteJsonOptions = {}
): Promise<void> { ): Promise<void> {
filePath = path.resolve(filePath);
let contentRaw = ""; let contentRaw = "";
try { try {
@ -38,8 +35,6 @@ export function writeJsonSync(
object: any, object: any,
options: WriteJsonOptions = {} options: WriteJsonOptions = {}
): void { ): void {
filePath = path.resolve(filePath);
let contentRaw = ""; let contentRaw = "";
try { try {