ngStorage: improve definitions and update to newest tslint (#16238)

* update ngstorage to new linter configuration

* update ngStorage interfaces

- remove the method StorageService.get() as it does not exist
- add the method StorageService.$supported() which was missing
- StorageService.$default() and StorageService.$reset() return an intersection type of its argument and StorageService
- add the method StorageProvider.remove() which was missing
- add the method StorageProvider.supported which was missing
This commit is contained in:
sclassen
2017-05-01 22:46:11 +02:00
committed by Mohamed Hegazy
parent 15d14159ba
commit 3440a18e98
4 changed files with 101 additions and 72 deletions

View File

@@ -1,23 +1,48 @@
// Type definitions for ngstorage 0.3.11
// Type definitions for ngstorage 0.3
// Project: https://github.com/gsklee/ngStorage
// Definitions by: Jakub Pistek <https://github.com/kubiq>
// Definitions: https://github.com/kubiq/DefinitelyTyped
declare module 'ngstorage' {
export namespace ngStorage {
interface StorageService {
/**
* @return true if the storage service is supported by the browser.
*/
$supported(): boolean;
/**
* Adds default values to the store.
* Copies all properties of the default items to the store.
* If the store already has one of these properties it will skip it.
*
* @param items object holding the default values.
* @return the modified storage service.
*/
$default<T>(items: T): StorageService & T;
/**
* Removes all properties from the store.
*
* @param items optional object holding the default values to initialize the store after reset.
* @return the modified storage service.
*/
$reset<T>(items?: T): StorageService & T;
interface IStorageService {
$default(items: {}): IStorageService;
$reset(items?: {}): IStorageService;
$apply(): void;
$sync(): void;
get<T>(key: string): T;
/**
* Access to the properties of the store.
*/
[key: string]: any;
}
interface IStorageProvider extends angular.IServiceProvider {
get<T>(key: string): T;
set<T>(key: string, value: T): T;
interface StorageProvider extends angular.IServiceProvider {
get<T>(key: string): T | boolean;
set<T>(key: string, value: T): T | boolean;
remove(key: string): void;
supported(): boolean;
setKeyPrefix(prefix: string): void;
setSerializer(serializer: (value: any) => string): void;
setDeserializer(deserializer: (value: string) => any): void;

View File

@@ -1,78 +1,81 @@
/// <reference types="angular"/>
import * as angular from "angular";
import { ngStorage } from "ngstorage";
import {IStorageService, IStorageProvider} from "ngstorage";
let app: angular.IModule = angular.module('at', ['ngStorage']);
var app: any;
app.controller('LocalCtrl', function ($localStorage: IStorageService) {
app.controller('LocalCtrl', ($localStorage: ngStorage.StorageService) => {
if ($localStorage.$supported()) {
const store: MyStore & ngStorage.StorageService = $localStorage.$default<MyStore>({
counter: 1
});
$localStorage.$default({
counter: 1
});
store.$reset<MyStore>({
counter: 1
});
$localStorage.$reset({
counter: 1
});
$localStorage.$reset();
$localStorage.$apply();
$localStorage.$sync();
store.$reset();
store.$apply();
store.$sync();
}
});
app.controller('SessionCtrl', function ($sessionStorage: IStorageService) {
app.controller('SessionCtrl', ($sessionStorage: ngStorage.StorageService) => {
if ($sessionStorage.$supported()) {
const store: MyStore & ngStorage.StorageService = $sessionStorage.$default<MyStore>({
counter: 1
});
$sessionStorage.$default({
counter: 1
});
store.$reset<MyStore>({
counter: 1
});
$sessionStorage.$reset({
counter: 1
});
$sessionStorage.$reset();
$sessionStorage.$apply();
$sessionStorage.$sync();
store.$reset();
store.$apply();
store.$sync();
}
});
app.config(['$localStorageProvider', function ($localStorageProvider: IStorageProvider) {
app.config(($localStorageProvider: ngStorage.StorageProvider) => {
if ($localStorageProvider.supported()) {
$localStorageProvider.setKeyPrefix('NewPrefix');
$localStorageProvider.set('MyKey', { counter: 'value' });
$localStorageProvider.get('MyKey');
$localStorageProvider.remove('MyKey');
$localStorageProvider.setKeyPrefix('NewPrefix');
const mySerializer = (value: any): string => {
return value.toString();
};
$localStorageProvider.get('MyKey');
const myDeserializer = (value: string): any => {
return value;
};
$localStorageProvider.set('MyKey', {counter: 'value'});
$localStorageProvider.setSerializer(mySerializer);
$localStorageProvider.setDeserializer(myDeserializer);
}
});
var mySerializer = function (value: any): string {
return value.toString();
};
app.config(($sessionStorageProvider: ngStorage.StorageProvider) => {
if ($sessionStorageProvider.supported()) {
$sessionStorageProvider.setKeyPrefix('NewPrefix');
$sessionStorageProvider.set('MyKey', { counter: 'value' });
$sessionStorageProvider.get('MyKey');
$sessionStorageProvider.remove('MyKey');
var myDeserializer = function (value: string): any {
return value;
};
const mySerializer = (value: any): string => {
return value.toString();
};
$localStorageProvider.setSerializer(mySerializer);
$localStorageProvider.setDeserializer(myDeserializer);
const myDeserializer = (value: string): any => {
return value;
};
$sessionStorageProvider.setSerializer(mySerializer);
$sessionStorageProvider.setDeserializer(myDeserializer);
}
});
interface MyStore {
counter?: number;
foo?: string;
}
]).config(['$sessionStorageProvider', function ($sessionStorageProvider: IStorageProvider) {
$sessionStorageProvider.setKeyPrefix('NewPrefix');
$sessionStorageProvider.get('MyKey');
$sessionStorageProvider.set('MyKey', {counter: 'value'});
var mySerializer = function (value: any): string {
return value.toString();
};
var myDeserializer = function (value: string): any {
return value;
};
$sessionStorageProvider.setSerializer(mySerializer);
$sessionStorageProvider.setDeserializer(myDeserializer);
}
]);

View File

@@ -7,7 +7,7 @@
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }