node06---npm、silly-datetime、路徑問題
阿新 • • 發佈:2017-06-17
for min length days ignore 需要 nod 都是 locale
我們剛才學習了,模塊就是一些功能的封裝,所以一些成熟的、經常使用的功能,都有人封裝成為了模塊。並且放到了社區中,供人免費下載。 這個偉大的社區,叫做npm。 也是一個工具名字 node package management https://www.npmjs.com/ 去社區搜索需求,然後點進去,看api。 如果要配置一個模塊,那麽直接在cmd使用 1npm install 模塊名字 就可以安裝。 模塊名字全球唯一。安裝的文件在一個node_modules文件夾中, 安裝的時候,要註意,命令提示符的所在位置。
08.js
var sd = require("silly-datetime");//需要使用一個日期時間,格式為 20150920110632 var ttt = sd.format(new Date(), ‘YYYYMMDDHHmm‘);
package.json
/* 跟08.js 在一個目錄。 我們可以用package.json來管理依賴。 在cmd中,08.js文件所在文件夾,使用npm init可以初始化一個package.json文件,用回答問題的方式生成一個新的package.json文件。 使用 08.js文件所在文件夾 : npm install 將能根據package.json安裝08.js所有的依賴。 npm也有文檔,這是package.json的介紹: https://docs.npmjs.com/files/package.json*/ { "name": "day2", "version": "1.0.0", "description": "ziji zuo de xia waner de", "main": "08.js", "directories": { "test": "test" }, "dependencies": { "silly-datetime": "^0.1.0" /*^表示固定只使用0.x.x的版本,不使用1.x.x,2.x.x*/ }, "devDependencies": {}, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" },"keywords": [ "good", "greet" ], "author": "kaola", "license": "ISC" }
require()別的js文件的時候,將執行那個js文件。 註意: require()中的路徑,是從當前這個js文件出發,根據相對路徑,找到別人。而fs是從命令提示符找到別人。 所以,桌面上有一個a.js, test文件夾中有b.js、c.js、1.txt a要引用b: var b = require(“./test/b.js”); b要引用c: var b = require(“./c.js”); 但是,fs等其他的模塊用到路徑的時候,都是相對於cmd命令光標所在位置。 所以,在b.js中想讀1.txt文件,推薦用絕對路徑: fs.readFile(__dirname + "/1.txt",function(err,data){ console.log(__dirname);//當前文件的絕對路徑,E:\360data\重要數據\桌面\test,跨平臺兼容,linux也可以兼容 if(err) { throw err; } console.log(data.toString()); });
silly-datetime源碼
(function(root, factory) { ‘use strict‘; /* istanbul ignore else */ if (typeof exports === ‘object‘) { // CommonJS module.exports = factory(); } else if (typeof define === ‘function‘ && define.amd) { // AMD define(function() { return factory(); }); } else if (typeof define === ‘function‘ && define.cmd) { // CMD define(function(require, exports, module) { module.exports = factory(); }); } else { // Global Variables root.ResizeImage = factory(); } })(this, function () { ‘use strict‘; var out = {}; /** * 將輸入的任意對象轉換成 Date,如果裝換失敗將返回當前時間 * @param {any} datetime 需要被格式化的時間 * @return {Date} 轉換好的 Date */ function getDateObject(datetime) { var t = datetime instanceof Date ? datetime : new Date(datetime); if (!t.getDate()) { t = new Date(); } return t; } /** * 格式化時間 * @param {Date} datetime 需要被格式化的時間 * @param {string} format 格式化字符串,默認為 ‘YYYY-MM-DD HH:mm:ss‘ * @return {string} 格式化後的時間字符串 */ out.format = function (datetime, format) { var t = getDateObject(datetime); var hours, o, i = 0; format = format || ‘YYYY-MM-DD HH:mm:ss‘; hours = t.getHours(); o = [ [‘M+‘, t.getMonth() + 1], [‘D+‘, t.getDate()], // H 24小時制 [‘H+‘, hours], // h 12小時制 [‘h+‘, hours > 12 ? hours - 12 : hours], [‘m+‘, t.getMinutes()], [‘s+‘, t.getSeconds()], ]; // 替換 Y if (/(Y+)/.test(format)) { format = format.replace(RegExp.$1, (t.getFullYear() + ‘‘).substr(4 - RegExp.$1.length)); } // 替換 M, D, H, h, m, s for (; i < o.length; i++) { if (new RegExp(‘(‘ + o[i][0] + ‘)‘).test(format)) { format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[i][1] : (‘00‘ + o[i][1]).substr((‘‘ + o[i][1]).length)); } } // 替換 a/A 為 am, pm return format.replace(/a/ig, hours > 11 ? ‘pm‘ : ‘am‘); }; /** * CONST and VAR for .fromNow */ // 預設語言:英語 var LOCALE_EN = { future: ‘in %s‘, past: ‘%s ago‘, s: ‘a few seconds‘, mm: ‘%s minutes‘, hh: ‘%s hours‘, dd: ‘%s days‘, MM: ‘%s months‘, yy: ‘%s years‘ }; // 預設語言:簡體中文 var LOCALE_ZH_CN = { future: ‘%s內‘, past: ‘%s前‘, s: ‘幾秒‘, mm: ‘%s分鐘‘, hh: ‘%s小時‘, dd: ‘%s天‘, MM: ‘%s月‘, yy: ‘%s年‘ }; // 當前本地化語言對象 var _curentLocale = {}; /** * 修改本地化語言 * @param {string|Object} string: 預設語言 `zh-cn` 或 `en`;Object: 自定義 locate 對象 * @return {Object} this */ out.locate = function (arg) { var newLocale, prop; if (typeof arg === ‘string‘) { newLocale = arg === ‘zh-cn‘ ? LOCALE_ZH_CN : LOCALE_EN; } else { newLocale = arg; } for (prop in newLocale) { if (newLocale.hasOwnProperty(prop) && typeof newLocale[prop] === ‘string‘) { _curentLocale[prop] = newLocale[prop]; } } return out; }; // 初始化本地化語言為 en out.locate(‘‘); /** * CONST for .fromNow */ // 各計算區間 var DET_STD = [ [ ‘yy‘, 31536e6 ], // 1000 * 60 * 60 * 24 * 365 一年月按 365 天算 [ ‘MM‘, 2592e6 ], // 1000 * 60 * 60 * 24 * 30 一個月按 30 天算 [ ‘dd‘, 864e5 ], // 1000 * 60 * 60 * 24 [ ‘hh‘, 36e5 ], // 1000 * 60 * 60 [ ‘mm‘, 6e4 ], // 1000 * 60 [ ‘s‘, 0 ], // 只要大於等於 0 都是秒 ]; /** * 計算給出時間和當前時間的時間距離 * @param {Date} datetime 需要計算的時間 * @return {string} 時間距離 */ out.fromNow = function (datetime) { var det = +new Date() - (+getDateObject(datetime)); var format, str, i = 0, detDef, detDefVal; if (det < 0) { format = _curentLocale.future; det = -det; } else { format = _curentLocale.past; } for (; i < DET_STD.length; i++) { detDef = DET_STD[i]; detDefVal = detDef[1]; if (det >= detDefVal) { str = _curentLocale[detDef[0]].replace(‘%s‘, parseInt(det/detDefVal, 0) || 1); break; } } return format.replace(‘%s‘, str); }; return out; });
node06---npm、silly-datetime、路徑問題