Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dongyuanxin committed May 17, 2019
1 parent cb88145 commit 03c7570
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 3 deletions.
1 change: 0 additions & 1 deletion README.md
Expand Up @@ -6,7 +6,6 @@
[![](https://img.shields.io/badge/release-v1.4.1-blue.svg?style=popout-square)](https://github.com/dongyuanxin/page-counter/issues)
[![](https://img.shields.io/badge/license-MIT-blue.svg?style=popout-square)](https://github.com/dongyuanxin/page-counter)


基于 `Serverless` 开发的的**极简网页计数器**,支持基于 `Hexo``Jekyll``Octopress``ReactJS``VueJS` 等框架开发的博客、网站、中后台等**任何应用**

## 特性
Expand Down
4 changes: 4 additions & 0 deletions src/config.js
Expand Up @@ -54,6 +54,10 @@ const bombRequired = [
}
]

/**
* Use many setters for dynamicly reading variables from window.
* Because variables may be loaded by async and after script
*/
class Config {
constructor() {}

Expand Down
9 changes: 9 additions & 0 deletions src/index.js
Expand Up @@ -11,6 +11,9 @@ copyright()
const platform = config.serverless
const serverless = new ServerLessFactory(platform)

/**
* Push page view meta to serverless cloud database
*/
async function setData() {
const date = new PowerDate()
const data = {
Expand All @@ -23,6 +26,9 @@ async function setData() {
serverless.setData(config[platform].table, data)
}

/**
* Count website total views and render it to '#page-counter-total-times' DOM
*/
async function countTotal() {
const dom = document.querySelector('#page-counter-total-times')
if (!dom) {
Expand All @@ -33,6 +39,9 @@ async function countTotal() {
dom.innerHTML = times + config[platform].history
}

/**
* Count single page views and render it to '#page-counter-single-times' DOM
*/
async function countSingle() {
const dom = document.querySelector('#page-counter-single-times')
if (!dom) {
Expand Down
15 changes: 15 additions & 0 deletions src/serverless/bomb.js
Expand Up @@ -11,9 +11,13 @@ class Hydrogen extends ServerLessInterface {
restApi
}
} = config
// Can't add Bomb to this. Otherwise, context error may occur.
Bomb.initialize(appId, restApi)
}

/**
* @return {*} public read and nobody write permission
*/
ACL () {
return {
'*': {
Expand All @@ -22,6 +26,11 @@ class Hydrogen extends ServerLessInterface {
}
}

/**
* Add data to table
* @param {String} table
* @param {Object} data
*/
async setData (table, data) {
const { Bomb } = config
const query = Bmob.Query(table)
Expand All @@ -40,6 +49,12 @@ class Hydrogen extends ServerLessInterface {
}
}

/**
* Calculate number of records in table.
* If url is not null, filter records whose URL is not equal to 'url'
* @param {String} table
* @param {String} url
*/
async count (table, url) {
const { Bomb } = config
const query = Bmob.Query(table)
Expand Down
4 changes: 4 additions & 0 deletions src/serverless/interface.js
@@ -1,3 +1,7 @@
/**
* Abstract interface for other serverless children class
* More platforms children class must extend this
*/
export default class ServerLessInterface {
constructor () {}

Expand Down
14 changes: 14 additions & 0 deletions src/serverless/leancloud.js
Expand Up @@ -15,13 +15,21 @@ class LeanCloud extends ServerLessInterface {
this.AV = AV
}

/**
* @return {*} public read and nobody write permission
*/
ACL () {
const acl = new this.AV.ACL()
acl.setPublicReadAccess(true)
acl.setPublicWriteAccess(false)
return acl
}

/**
* Add data to table
* @param {String} table
* @param {Object} data
*/
async setData (table, data) {
const Obj = this.AV.Object.extend(table)
const obj = new Obj()
Expand All @@ -40,6 +48,12 @@ class LeanCloud extends ServerLessInterface {
}
}

/**
* Calculate number of records in table.
* If url is not null, filter records whose URL is not equal to 'url'
* @param {String} table
* @param {String} url
*/
async count (table, url) {
const query = new this.AV.Query(table)
if (typeof url === 'string' && url.length > 0) {
Expand Down
18 changes: 16 additions & 2 deletions src/utils.js
@@ -1,5 +1,13 @@
import pkg from './../package.json'

/**
* Example:
* ```javascript
* addZeroStr(1) // '01'
* addZeroStr(32) // '32'
* ```
* @param {Number} num
*/
function addZeroStr(num) {
if (num < 10) {
return '0' + num
Expand All @@ -8,6 +16,9 @@ function addZeroStr(num) {
}
}

/**
* Add format function to Date Object
*/
export function PowerDate() {
const date = new Date()

Expand All @@ -28,7 +39,7 @@ export function PowerDate() {
}

/**
*
* Add '/' to url both sides
* @param {String} url
*/
export function formatURL(url) {
Expand All @@ -46,7 +57,7 @@ export function formatURL(url) {
}

/**
*
* Validate obj as options requires
* @param {Object} obj
* @param {Array} options
*/
Expand All @@ -73,6 +84,9 @@ export function validator(obj, options) {
return [true]
}

/**
* Print copyright in console
*/
export function copyright() {
console.log(
`\n%c ${pkg.name} v${pkg.version} %c` +
Expand Down

0 comments on commit 03c7570

Please sign in to comment.